ajax上传图片,springboot接受报错?
当上传图片之后,后台报错:Current request is not a multipart request.网上找过解决方案,说是将contentType改成"multipart/form-data",结果又是报错:the request was rejected because no multipart boundary was found.试过用form表单提交,可以成功上传图片。求大佬解答用ajax怎么上传{:1_937:} 转成BASE64试试 https://cloud.tencent.com/developer/article/1734696照抄即可,目测你的data有问题,不应该写成 data : { file: formData}
而是 改成 data: formData 下面这个是我之前试过的,和你的差别在ajax中data,我的直接是formData,你多封装了一层
function uploadFile() {
var formData = new FormData();
formData.append("photo", $("#file").files);
formData.append("type_id", 1);
$.ajax({
url: '/upload', /*接口域名地址*/
type: 'post',
data: formData,
contentType: false,
processData: false,
success: function (res) {
console.log(res);
}
})
}
Todd 发表于 2022-4-21 22:59
https://cloud.tencent.com/developer/article/1734696
照抄即可,目测你的data有问题,不应该写成 data : ...
发现问题了,data:{file: formData}是个错误,其次我在formData.append()中的name写错成了files。。应该是file
删除一个funtion,然后下面的files去掉一个s看看,你这个不是说方法不被允许吗
https://www.cnblogs.com/daen/p/16002864.html 按照楼主的参数,这样就可以了
function uploadFile() {
var formData = new FormData();
formData.append("file", $("#file").files);
$.ajax({
url: '/upload', /*接口域名地址*/
type: 'post',
data: formData,
contentType: false,
processData: false,
success: function (res) {
console.log(res);
}
})
}
页:
[1]