首先上图:
点击文件上传按钮,上传文件的同时,还要传递 项目id 的参数 projectId。网上很多文章都是只传递 formdata 对象,而不携带其他参数,即使是这样需求,也有很多网友碰到问题,都是说要加 ‘Content-Type‘: ‘multipart/form-data‘ 的。但是怎么携带其他参数呢?
后来看到一篇文章,原文链接:
https://blog.csdn.net/weixin_43173924/article/details/89395537
说的是其他参数携带在头部里面,然后自己改写的代码如下(完全参照文章的方法):
//文件上传
handleChange(file) {
let fd = new FormData();
fd.append(‘file‘,file.raw);//文章中取的值是 file.raw
fetch(url, {
method: ‘POST‘,
headers: {
‘Content-Type‘: ‘multipart/form-data‘,
//文章说参数放到其他地方后端拿不到,所以把参数 projectId 放在头部传递
‘projectId‘: this.projectId
},
data:fd
})
.then(res=>res.json())
.then(res=>{console.log(res)})
.catch(function(e) {
console.log("error");
});
}