7***@qq.com
7***@qq.com
  • 发布:2022-07-20 22:59
  • 更新:2022-07-21 15:59
  • 阅读:851

uni.uploadFile 上传到springboot服务器不行

分类:uni-app

服务器报错:
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/json
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:169)

怎么改都不行, 请问到底什么问题?

uni.uploadFile({  //huUploadImage  
        url: appendApi(obj.url),  
        header: appendHeader(obj.header),  
        files: obj.files,  
        success: (res) => {  
            // {"code":0,"message":"成功","data":[{"code":0,"filename":"upload/userInfo/1/C95C5769BD1CE9C98A95EBAD0E8B95F4.jpeg","message":"success"}]}  
            if (typeof (res.data) === "string") {  
                res.data = JSON.parse(res.data)  
            }  
            httpCallBackSuc(res, obj);  
        },  
        fail: (res) => {  
            httpCallBackFail(res, obj);  
        }  
    });  

 uni.chooseImage({  
                count: 1,  
                success: (chooseImageRes) => {  
                    console.log(chooseImageRes);  
                    huUploadImage({  
                        files: chooseImageRes.tempFiles,  
                        header: header,  
                        suc: (res) => {  
                            const dd = res.data[0];  
                            if (dd.code == 0) {  
                                Editor.insertEmbed(cursorLocation, "image", appendHost(dd.filename));  
                            } else {  
                                self.showOkDialog("错误", "上传错误,信息:" + res)  
                            }  
                            resetUploader();  
                        }, fail: (err) => {  
                            console.log(err);  
                            self.showToastNoIcon("发生错误:" + err)  
                        },  
                    });  
                }  
            });
2022-07-20 22:59 负责人:无 分享
已邀请:
7***@qq.com

7***@qq.com (作者)

看起来无法删除, 那我就写个答案吧.

参考如下代码, 问出出在appendHeader , appendHeader这个函数post/get都在用, 关键点是: // let h1 = httpConfig.header; if (!h1) h1 = {} 这行代码是有问题的. 这行代码最终返回的是其实是 httpConfig.header 的指针

然后 使用了 uni.request的 post/get请求时... uni.request 请求的 header 中 content-type 默认为 application/json。 uni.request 会设置header的content-type

最终就是在uploadfile时, 我的header里就带有 application/json, 导致uploadfile 无法自己设置 formdata 就gg了..

function appendHeader (header) {
if (!header && !httpConfig.header)
return null; //都没有设定
// let h1 = httpConfig.header; if (!h1) h1 = {}
let h1 = null
if (httpConfig.header) h1 = { ...httpConfig.header }
else h1 = {}
if (header) return Object.assign(h1, header)
else return h1
}

export const uploadFileWithPath = function (obj) {  
    uni.uploadFile({  
        url: appendApi(obj.url),  
        header: appendHeader(obj.header),  
        filePath: obj.filePath,  
        name: "file",  
        success: (res) => {  
            // {"code":0,"message":"成功","data":[{"code":0,"filename":"upload/userInfo/1/C95C5769BD1CE9C98A95EBAD0E8B95F4.jpeg","message":"success"}]}  
            if (typeof (res.data) === "string") {  
                res.data = JSON.parse(res.data)  
            }  
            httpCallBackSuc(res, obj);  
        },  
        fail: (res) => {  
            httpCallBackFail(res, obj);  
        }  
    });  
}  

该问题目前已经被锁定, 无法添加新回复