vue + Element上传多个文件,支持删除重新上传,上传后将后台返回的URL发送给后台存到数据库
<el-col :span="24">
<el-form-item label="上传附件:" prop="attachmentUrls">
<el-upload
multiple :limit="3"
class="upload-demo"
:action="doUrl"
:on-exceed="handleExceed"
:before-upload="beforeUploadPicture"
:on-success="uploadSuccess"
:on-remove="handleRemove"
:file-list="fileList" :show-file-list="true">
<el-button size="small" type="primary">点击上传</el-button>
</el-upload>
</el-form-item>
</el-col>
data () {
return {
visible: false,
doUrl: '',
fileList: [],
dataForm: {
id: '',
version: '',
closeNum: 1,
settleIds: '',
dealResult: 1,
sendObj: '区农经站主管部门',
promptRemark: '',
attachmentUrls: ''
}
}
},
methods: {
init () {
this.doUrl = `${window.SITE_CONFIG['apiURL']}/sys/oss/upload?token=${localStorage.getItem('token')}`
this.visible = true
},
handleExceed (files, fileList) {
return this.$message.warning('当前限制最多可以上传3个文件')
},
handleRemove (file,fileList) {
this.fileChange(fileList);
},
uploadSuccess (res, file, fileList) {
this.fileChange(fileList);
},
fileChange(file,fileList) {
let tempStr = '';
if (fileList.length > 0) {
for (let i = 0; i < fileList.length; i++) {
if (fileList[i].response) {
if (fileList[i].response.code === 0) {
if (i === 0) {
tempStr += fileList[i].response.data.src;
} else {
tempStr += ',' + fileList[i].response.data.src;
}
}
} else if (fileList[i].status && fileList[i].status === 'success') {
if (i === 0) {
tempStr += fileList[i].url;
} else {
tempStr += ',' + fileList[i].url;
}
}
}
}
this.dataForm.attachmentUrls = tempStr;
},
beforeUploadPicture (file) {
const is10M = file.size / 1024 / 1024 < 10
if (!is10M) {
this.$message.error('文件大小不能超过10MB!')
return false
}
return true
},
dataFormSubmitHandle: debounce(function () {
this.$refs.dataForm.validate((valid) => {
if (!valid) {
return false
}
if (!this.dataForm.id) {
return false
}
this.$http.post('townEarlyWarningEvent/townEarlyWarningEvent/byHandle', this.dataForm).then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.$message({
message: this.$t('prompt.success'),
type: 'success',
duration: 500,
onClose: () => {
this.visible = false
this.$emit('refreshDataList')
}
})
}).catch(() => {})
})
}, 1000, { leading: true, trailing: false })
}