onChange(file, fileList) { //這里做一些文件控制,注意:就算一次選取多個文件,這里依舊會執行多次
const isJPG = file.raw.type === 'image/jpeg';
if (!isJPG) {
this.$message.error('上傳頭像圖片只能是 JPG 格式!');
fileList.pop()
}
let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name)
if (existFile) {
this.$message.error('當前文件已經存在!');
fileList.pop()
}
this.fileList = fileList
},
onRemove(file, fileList) {
this.fileList = fileList
},
OnExceed(file, fileList) {
this.$message.error(`最多只能上傳10張圖片!`);
},
submitUpload() { //上傳函數,如果把提交函數用在http-request鈎子中,fileList多長就執行請求多少次接口,依舊是無法做到只請求一次上傳多文件
var formData = new FormData(); // 用FormData存放上傳文件
this.fileList.forEach(file => {
formData.append('reportFile', file.raw, file.raw.name); //此處一定是append file.raw 上傳文件只需維護fileList file.raw.name要加上
})
uploadFiles(formData).then(res => { //手動上傳貌似無法觸發成功或失敗的鈎子函數,因此這里手動調用
this.onSuccess()
}).catch(err => {
console.log(err)
this.onError()
})
}