<el-upload
class="upload-demo"
ref="upload"
name="file"
action="你的url"
:file-list="fileList"
:auto-upload="false">
<el-button slot="trigger" size="small" type="primary">浏览</el-button>
</el-upload>
1.使用formData的格式发送请求:
let formData = new FormData(); // 用FormData存放上传文件
for(let i=0;i< this.fileList.length;i++){
let testFile= this.fileList[i].raw;
formData.append('file', testFile);
}
console.log(formData.get('file'));
2.请求:
pageApi.uploadFiles(formData).then(res =>{
if(res.data.date.message === "success"){
this.$message.error('操作成功');
}else{
this.$message.error('操作失败');
}
}).catch(() => {
this.$message.error('操作失败');
})
3.请求js:
// 文件上传
uploadFiles: async function (params) {
return request({
url: '你的url',
method: 'post',
contentType: false,//这里不要落下
dataType: 'json',
data:params
})
}
4.后端--红框处即可收到文件内容