uni-app uploadFile,downloadFile等方法 filePath中含有特殊字符导致报错400

uni-app uploadFile,downloadFile等方法 filePath中含有特殊字符导致报错400

测试过的手机:

iphonexr,小米11青春版

操作步骤:

  • 通过文件选择,然后获取含有中文的路径后调用downloadFIle或者uploadFile 报错400

预期结果:

  • 应该正常上传或下载

实际结果:

  • 通过文件选择,然后获取含有中文的路径后调用downloadFIle或者uploadFile 报错400

bug描述:

  • uploadFIle,downloadFile等方法中,如果filePath路径中含有中文或者其他特殊字符,会导致无法上传或者下载,但是不会进入到错误信息,返回状态statusCode:400

img


更多关于uni-app uploadFile,downloadFile等方法 filePath中含有特殊字符导致报错400的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

将地址编码后试试

更多关于uni-app uploadFile,downloadFile等方法 filePath中含有特殊字符导致报错400的实战教程也可以访问 https://www.itying.com/category-93-b0.html


尝试使用escape和encodeURI等编码方法后,依然是报400错误

这个bug确认了吗 啥时候可以修复下呀

报错信息如下图

这个问题是由于URL编码导致的。当filePath包含中文或其他特殊字符时,需要正确编码才能正常使用。

解决方案:

  1. 对filePath进行编码处理:

    // 上传文件示例
    uni.uploadFile({
        url: 'https://example.com/upload',
        filePath: encodeURI(filePath), // 关键:对路径进行编码
        name: 'file',
        success: (res) => {
            console.log(res.data);
        }
    });
    
  2. 对于下载文件同样适用:

    uni.downloadFile({
        url: 'https://example.com/file',
        filePath: encodeURI(savePath), // 保存路径也需要编码
        success: (res) => {
            if (res.statusCode === 200) {
                console.log('下载成功');
            }
        }
    });
回到顶部