风中人
风中人
  • 发布:2022-07-16 17:10
  • 更新:2022-09-15 21:42
  • 阅读:3409

uni.uploadFile怎么设置header的content-type

分类:uni-app

我在开发一款产品,在文件上传这里遇到了问题,官方的uni.uploadFile的content-type默认为multipart/form-data,改不了, 在程序中设置了其他的content-type,也会强制转换成content-type/form-data。而我开发产品的服务端要求用application/octet-stream,这个服务端我也改不了,哪痊大神知道怎么处理吗?https://uniapp.dcloud.io/api/request/network-file.html#uploadfile 这里是官方的说明。

2022-07-16 17:10 负责人:无 分享
已邀请:
xzh

xzh

碰到一样的问题,下面是我的解决方案

uni.chooseImage({  
    sourceType: ['camera'], //从相册选择  
    success: function ({ tempFilePaths }) {  
        let tempFilePath = tempFilePaths[0];  
        plus.io.resolveLocalFileSystemURL(  
            tempFilePath,  
            function (entry) {  
                entry.file(function (file) {  
                    const reader = new plus.io.FileReader();  
                    reader.readAsDataURL(file);  
                    // 文件读取操作完成时的回调函数  
                    reader.onloadend = (fileData) => {  
                        const speech = fileData.target.result; //base64图片  
                        uni.request({  
                            url: '',  
                            method: 'POST',  
                            header: {  
                                'content-type': 'application/octet-stream'  
                            },  
                            data: uni.base64ToArrayBuffer(  
                                // 去除base64文件头  
                                speech.replace(  
                                    /^data:image\/\w+;base64,/,  
                                    ''  
                                )  
                            ),  
                            success(res) {  
                                console.log(res);  
                            }  
                        });  
                    };  
                });  
            }  
        );  
    }  
});
DCloud_UNI_WZF

DCloud_UNI_WZF

使用 uni.request 试试呢

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