<el-upload ref="uploadDemo"
class="upload-demo"
multiple
accept=".docx,.doc,.pdf"
:headers="headerData"
:action="actionUrl"
:file-list="fileList"
:before-upload="beforeUpload"
:on-success="onSuccess"
:on-error="onError">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">支持上传文件格式:doc .docx .pdf,文件大小不超过2M</div>
</el-upload>
data:{
return{
photoList: [],
photoList2:[],
actionUrl: this.$config.VUE_APP_API_HOST_DEFAULT + '/file/upload',
}
}
computed: {
/****以下文件上传所用**/
headerData() {
return {
Authorization: getAccessToken(),
timestamp: String(new Date().getTime()),
sign: ''
}
}
},
methods:{
/*****以下文件上传所用*******/
beforeUpload(file, fileList) {
console.log(file, fileList)
let flag = true
this.photoList.forEach(item=> {
if (item.name === file.name) {
this.$message.error('"'+file.name + '"文件重复');
flag = false
}
})
return flag
},
onSuccess(response, file, fileList) {
console.log(response, file, fileList)
if (response && response.code === 200) {
this.photoList2.unshift({
id: response.data.fileId,
name: file.name,
size: file.size
})
} else {
this.$message.error(response ? response.msg : '文件上传失败');
if (fileList) {
fileList.forEach((item,index)=> {
if (item.response && item.response.code !== 200) {
fileList.splice(index,1)
}
});
}
}
if (fileList.length === this.photoList2.length) {
this.$refs['uploadDemo'].clearFiles()
this.photoList = this.photoList2.concat(this.photoList)
this.photoList2 = []
}
},
onError(e, file, fileList) {
console.log(e, file, fileList)
this.$message.error('"'+file.name +'"上传失败');
},
}