Fixed: Image Upload

This commit is contained in:
zhaojisen 2025-03-13 15:29:22 +08:00 committed by ZhaoJiSen
parent aaad6b8380
commit 446cb0b85e

View File

@ -10,6 +10,9 @@
<div> <div>
<img v-if="preview" :class="showBG ? 'show-bg' : ''" :src="preview" v-bind="$attrs" alt=""> <img v-if="preview" :class="showBG ? 'show-bg' : ''" :src="preview" v-bind="$attrs" alt="">
</div> </div>
<el-button v-if="fileName" size="mini" type="danger" @click.native.stop="resetUpload">
{{ this.$t('Cancel') }}
</el-button>
</div> </div>
</template> </template>
@ -55,21 +58,29 @@ export default {
}, },
Onchange(e) { Onchange(e) {
const upLoadFile = e.target.files[0] const upLoadFile = e.target.files[0]
if (upLoadFile === undefined) { if (upLoadFile === undefined) {
this.$emit('input', this.initial)
return return
} }
this.fileName = upLoadFile?.name || '' this.fileName = upLoadFile?.name || ''
this.$emit('fileChange', upLoadFile) this.$emit('fileChange', upLoadFile)
this.$emit('input', this.getObjectURL(upLoadFile)) this.$emit('input', this.getObjectURL(upLoadFile))
}, },
resetUpload() {
this.fileName = ''
this.preview = ''
this.$refs.upLoadFile.value = ''
this.$emit('input', '')
this.$emit('fileChange', null)
},
getObjectURL(file) { getObjectURL(file) {
let url = null let url = null
if (window.createObjectURL !== undefined) { // basic if (window.createObjectURL !== undefined) {
url = window.createObjectURL(file) url = window.createObjectURL(file)
} else if (window.URL !== undefined) { // mozilla(firefox) } else if (window.URL !== undefined) {
url = window.URL.createObjectURL(file) url = window.URL.createObjectURL(file)
} else if (window.webkitURL !== undefined) { // webkit or chrome } else if (window.webkitURL !== undefined) {
url = window.webkitURL.createObjectURL(file) url = window.webkitURL.createObjectURL(file)
} }
return url return url