uni-app uni.downloadFile 当文件名包含= .等特殊字符时,导致下载后无法打开

uni-app uni.downloadFile 当文件名包含= .等特殊字符时,导致下载后无法打开

开发环境 版本号 项目创建方式
Windows WIN10 HBuilderX

示例代码:

download:function(e,index){   
    var that=this;  
    //#ifdef H5  
        // uni.hideLoading()  
        window.location.href=admin.host+"approvalFormData/downloadFile/"+e  
    //#endif  
    //#ifndef H5   
    uni.showLoading()  
    uni.downloadFile({   
        url: admin.host+"approvalFormData/downloadFile/"+e,   
        success: (res) => {  
            console.log(res)  
                 uni.saveFile({  
                      tempFilePath: res.tempFilePath,  
                      success: function (resx) {  
                            var downlists=that.downlist  
                            downlists=downlists.filter(function(item){  
                                return item.id != e   
                            })  
                            downlists.push({id:e,filePath:resx.savedFilePath})  
                            var listarray=that.listarray  
                            console.log(resx.savedFilePath)  
                            listarray[index].filePath=resx.savedFilePath;  
                            listarray[index].zhuantai=true;  
                            that.listarray = listarray  
                            uni.setStorage({  
                                key:'spfj' + that.spid,  
                                data: downlists,  
                                success: function () {   
                                    uni.hideLoading()  
                                }  
                            });  

                      },fail(resx) {  
                        console.log(resx)  
                      }  
                });  
        },  
        fail:(res) => {  
            console.log(res)  
        }  
    });  
    //#endif  
}


# 操作步骤:

下载  以该名称命名的图片1612660462400_src=http://a2.att.hudong.com/86/1001300000184180121920108394217.jpg&refer=http://a2.att.hudong.com.jpg 

# 预期结果:

```json
{
"tempFilePath": "_doc/uniapp_temp_1615252340096/download/1612660462400_src=http://a2.att.hudong.com/86/1001300000184180121920108394217.jpg&refer=http://a2.att.hudong.com.jpg",
"statusCode": 200,
"errMsg": "downloadFile:ok"
}

实际结果:

错误返回
{
"tempFilePath": "_doc/uniapp_temp_1615252340096/download/1612660462400_src",
"statusCode": 200,
"errMsg": "downloadFile:ok"
}

bug描述:

uni.downloadFile 下载文件,当文件名称内包含 = .等特殊字符。会导致下载成功后的tempFilePath 缺失,导致文件无法打开。

该文件名称为: 1612660462400_src=http://a2.att.hudong.com/86/1001300000184180121920108394217.jpg&refer=http://a2.att.hudong.com.jpg

错误返回

{
"tempFilePath": "_doc/uniapp_temp_1615252340096/download/1612660462400_src",
"statusCode": 200,
"errMsg": "downloadFile:ok"
}

更多关于uni-app uni.downloadFile 当文件名包含= .等特殊字符时,导致下载后无法打开的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app uni.downloadFile 当文件名包含= .等特殊字符时,导致下载后无法打开的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的URL编码问题。当文件名包含=.等特殊字符时,uni.downloadFile在解析URL参数时会出现截断,导致tempFilePath不完整。

从你的代码可以看到,实际返回的tempFilePath只到1612660462400_src就被截断了,这是因为URL中的=被当作参数分隔符处理。

解决方案是对文件名进行URL编码:

// 在下载前对文件名进行编码
let encodedFileName = encodeURIComponent(e);
uni.downloadFile({   
    url: admin.host + "approvalFormData/downloadFile/" + encodedFileName,   
    success: (res) => {
        // ... 其他代码保持不变
    }
});
回到顶部