<el-upload style="display: inline-block" :limit="6"
class="upload-demo" action="#" ref="upload" multiple
:file-list="fileList" :on-change="handleChange"
:on-exceed="handleExceed" :before-upload="beforeAvatarUpload"
:http-request="uploadSectionFile" :auto-upload="false"
:on-remove="handleRemoveAttachment" :before-remove="handleRemove">
<span slot="trigger" class="selectFile">选取文件</span> </el-upload>
<el-button @click='uploadFiles'>上传<el-button>
//附件上传
// 上传前校验
beforeAvatarUpload(file) {
console.log(file)
const isLt2M = file.size / 1024 / 1024 < 5
if (!isLt2M) {
this.$message.error('上传图片大小不能超过 5MB!')
}
return isLt2M
},
// 超过上传limit提示
handleExceed(files, fileList) {
this.$message.warning(`最多上传 ${this.limit} 个文件`)
},
//文件删除提示
handleRemove(file) {
return this.$confirm(`确定移除 ${file.name}?`).then(() => {
this.$message.success('删除')
})
},
//根据新增 删除还是编辑 删除 编辑的附件是存到数据库的需要调接口
handleRemoveAttachment(file, fileList) {
console.log(file)
if (this.resourceForm.id) {
delAttach({ id: file.id }).then((res) => {
if (res.data.status === 1) {
this.$message.success('删除成功!')
this.getTablelist()
}
})
} else {
for (var i = 0; i < this.fileList.length; i++) {
if (file.uid == this.fileList[i].uid) {
this.fileList.splice(i, 1)
}
}
}
},
handleChange(file, fileList) {
this.fileList = fileList
console.log(this.fileList)
},
//文件上传接口
uploadSectionFile(param) {
//附件
let formData = new FormData()
var fileObj = param.file
formData.append('files', fileObj)
//后端需要的参数
formData.append('attachTable', 'device')
formData.append('tableId', this.fileId)
//不要加{ formData } !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
fileUploads(formData).then((res) => {
if (res.data.status === 1) {
} else {
this.$message.error('附件上传失败!')
}
})
},
// 上传
uploadFile() {
this.$refs.upload.submit()
},