easybbseasybbs上传文件匡思进2025-09-232025-10-06上传文件123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687<script setup>import { ref, getCurrentInstance } from 'vue';const {proxy} = getCurrentInstance()const props = defineProps({ modelValue: { type: Object, default: null }, imageUrlPrefix: { type: String, }})//如果父组件传来一个图片地址, 就用父组件的, 否则展示用户上传的图片const localPreview = ref(false)const localFile = ref(null)const emit = defineEmits(['update:modelValue'])const selectorFile = async(file) => { file = file.file let img = new FileReader() img.readAsDataURL(file) img.onload = ({target}) => { localFile.value = target.result } localPreview.value = true emit("update:modelValue", file)}</script><template><div class="attachment-selector"> <el-upload name="file" :show-file-list="false" accept=".zip, .rar" :multiple="false" :http-request="selectorFile" > <div class="attachment-selector-btn"> <template v-if="localFile"> <img :src="localFile"> </template> <template v-else> <img :src="(imageUrlPrefix ? imageUrlPrefix : proxy.globalInfo.imageUrl) + modelValue.imageUrl" v-if="modelValue && modelValue.imageUrl" > <span class="iconfont icon-add"></span> </template> </div> </el-upload> <template v-if="modelValue"> <div class="file-name" :title="modelValue.name"> {{ modelValue.name }} </div> <div class="iconfont iconfont-del"></div> </template></div></template><style scoped>.attachment-selector { .attachment-selector-btn { background: #dddddd3d; width: 150px; height: 150px; display: flex; align-items: center; justify-content: center; img { max-width: 100%; height: auto; } .iconfont { font-size: 50px; color: #ddd; } }}</style> 上传附件1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071<script setup>import { getCurrentInstance } from 'vue';const {proxy} = getCurrentInstance()const props = defineProps({ modelValue: { type: Object, default: null }, imageUrlPrefix: { type: String, }})const emit = defineEmits(['update:modelValue'])const selectorFile = async(file) => { file = file.file emit("update:modelValue", file)}const delFile = () => { emit("update:modelValue", null)}</script><template><div class="attachment-selector"> <el-upload name="file" :show-file-list="false" accept=".zip, .rar" :multiple="false" :http-request="selectorFile" > <el-button type="primary" v-if="!modelValue">上传附件</el-button> </el-upload> <template v-if="modelValue"> <div class="file-name" :title="modelValue.name"> {{ modelValue.name }} </div> <div class="iconfont icon-del" @click="delFile" ></div> </template></div></template><style scoped>.attachment-selector { width: 100%; display: flex; .iconfont { cursor: pointer; font-size: 19px; color: var(--link); } .file-name { white-space: nowrap; overflow: hidden; color: #9b9b9b; flex: 1; }}</style>