<el-upload ref="upload"
action=""
multiple
:before-remove="beforeRemove"
:before-upload="beforeUpload"
:on-change="onChange"
:http-request="uploadFile"
>
<el-button size="mini" :loading="uploading">上传文件</el-button>
<span slot="tip" class="el-upload__tip">最大不超过20M</span>
</el-upload>
methods:
// 删除文件
beforeRemove(file, fileList){
return this.$confirm(`确定移除 ${ file.name }?`).then(() => {
this.fileList = fileList.filter(obj => obj.uid != file.uid)
return true
}).catch(()=>{
return false
})
},
// 文件变化
onChange(file, fileList){
file.status = 'success'
this.fileList = fileList
},
// 自定义上传事件。如果需要在选择文件后就上传,可在此自定义
uploadFile(file){
// console.log(file)
},
接口参数结构如下:
{
"fileList": [
<file>, //是file对象
<file>,
<file>
],
"key_a": '',
"key_b": ''
}
!!!注意:因为后台要求参数整体是一个formData对象,上传的文件放到一个文件数组中。
接口传参为:
let data = {
key_a: this.form.a, //其他表单项
fileList: this.fileList, //上传的文件集合
}
let formdata = new FormData()
for(let key in data){
if(key == 'fileList'){
data.fileList.forEach((obj, index) => {
// 注意:此处的key要传fileList[index]
formdata.append('fileList['+index+']', obj)
})
}else{
formdata.append(key, data[key])
}
}
调用接口成功后注意表单重置及清空上传文件列表 !
//清空表单
this.$refs.form.resetFields()
//清空文件列表
this.$refs.upload.clearFiles()