传MultipartFile[] files,遍历files存到硬盘就可以
上传文件列表
openEditDialog(row){
this.fileList.splice(0, this.fileList.length);
row.filePathList.forEach(item => {
const filePath = item.filePath;
const fileName = item.fileName;
this.fileList.push({
filePath:filePath,
name:fileName
});
}
}
fileChange(file, fileList) {
this.fileList = fileList; //直接赋值就可以
}
fileChange(file, fileList) {
let a = 0;
fileList.forEach((item, idx) => {
/*在此处,对比文件名,将文件名相同的对比次数累加,
相同的文件名累加值为 2 时,说明文件名已经重复,直接删掉。*/
if (file.name === item.name) {
a++;
if (a === 2) {
this.$message({message: '文件名不能重复',type: 'info'});
fileList.splice(idx, 1);
}
}
})
this.fileList = fileList;
}
onRemoveFile(file) {
let fileList = this.fileList;
for(let j=fileList.length-1;j>=0;j--){
if(fileList[j].name==file.name) {
fileList.splice(j,1);
break;
}
}
}
openEditDialog(row){
this.deleteFilePaths.splice(0,this.deleteFilePaths.length);
}
onRemoveFile(file) {
/*保存的时候才触发删除,不然没有保存就把文件删除了*/
const filePath =file.filePath;
if(filePath!=undefined&&filePath!=null&&filePath!=""){//已经上传服务器的文件才删除
this.deleteFilePaths.push(filePath);
}
}
向后端发送删除请求
async batchDeleteFile(){
if(this.deleteFilePaths==undefined||this.deleteFilePaths==null||this.deleteFilePaths.length==0) return;
await batchDeleteFile(this.deleteFilePaths).then(()=>{}).catch(()=>{
this.$message({type: "error",message: res.message});
})
}
自己写函数项后端发送请求。上传文件函数。多个文件用 fd.append(‘files’, file.raw)。利用file.raw不为空,过滤掉已有文件。只上传新增文件
async fetchBatchUpload(){
const fd = new FormData();
for (const file of this.fileList) {//多个文件全部都放到files
if(file.raw) {
fd.append('files', file.raw);
}
}
if(fd.get("files")==null||fd.get("files")==undefined) return;
fd.append('modelType', 'N');
fd.append('categoryType','');
fd.append('month','');
fd.append('year','');
await batchUploadFile(fd).then(res =>{
this.filePathList = this.filePathList.concat(res.data.data);
});
},
:auto-upload="false"
multiple
<el-button type="primary" @click="save()">保存</el-button>
async save(){
//组装数据
//向后端发送请求
}
openEditDialog(row){
this.filePathList.splice(0,this.filePathList.length);
row.filePathList.forEach(item => {
const filePath = item.filePath;
const fileName = item.fileName;
this.filePathList.push("filePath="+filePath+";fileName="+fileName);
}
}
await batchUploadFile(fd).then(res =>{
this.filePathList = this.filePathList.concat(res.data.data);
});
onRemoveFile(file) {
let filePathList = this.filePathList;
for(let i=filePathList.length-1;i>=0;i--){
let indexFilePath =filePathList[i].indexOf(file.name);
if(indexFilePath!=-1) {
filePathList.splice(i,1);
break;
}
}
}
await 定义batchDeleteFile()方法可以等向后端发送完请求再往下执行,async与await同步出现。所以async定义batchDeleteFile方法
async batchDeleteFile(){
if(this.deleteFilePaths==undefined||this.deleteFilePaths==null||this.deleteFilePaths.length==0) return;
await batchDeleteFile(this.deleteFilePaths).then(()=>{}).catch(()=>{
this.$message({type: "error",message: res.message});
})
},
调用 batchDeleteFile 要加await,async与await成对出现,所以async save()
async save(){
await this.batchDeleteFile();
await this.fetchBatchUpload();
updatenotice(this.form,this.filePathList).then(() => {
})
}