d***@163.com
d***@163.com
  • 发布:2021-10-19 15:57
  • 更新:2021-10-21 10:13
  • 阅读:5576

【报Bug】UNIAPP 使用uni.downloadFile方法下载之后用uni.openDocument方法报错找不到文件

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 11.5.2

HBuilderX类型: 正式

HBuilderX版本号: 3.2.3

手机系统: iOS

手机系统版本号: iOS 12.4

手机厂商: 苹果

手机机型: iphone6p

页面类型: vue

打包方式: 离线

项目创建方式: HBuilderX

示例代码:

downloadImage(item) {

            uni.downloadFile({  

                url: 'http://localhost:40003/file/service-file/download/'+item.fileStorageNo,  

                 success: function (res) {  
                    // var filePath = escape(res.tempFilePath);  
                    var filePath = res.tempFilePath;  
                    uni.showModal({  
                        title:filePath,  
                    });  
                    console.log(filePath)  
                    uni.openDocument({  
                      filePath:filePath,  
                      fileType:"pdf",  
                      success: function (res) {  
                           uni.hideLoading();  
                           this.downloadFile_onoff = true;  
                          console.log('打开文档成功');  
                      },  
                      fail(err){  
                         uni.showModal({  
                            title:err.errMsg,  
                         });  
                           uni.hideLoading();  
                           this.downloadFile_onoff = true;  
                      }   
                    });  
                  }  
            })  
        },

操作步骤:

点击文件进行查看,先输出文件地址,执行uni.openDocument方法时提示找不到文件

预期结果:

打开文件并预览

实际结果:

提示错误信息

bug描述:

使用以下代码提示如下

2021-10-19 15:57 负责人:无 分享
已邀请:
d***@163.com

d***@163.com (作者)

问题已解决,由于后台业务需求需要将文件放在生成的文件夹中并且文件名称需要使用文件夹名称 + ‘—’ + 文件名使用,ios中的uni.openDocument 方法不能识别下划线导致文件读取失败。
解决办法建议:使用ngix配置,将文件访问地址配置成一个外网可以访问的地址再进行下载并打开。

                                var filePath = res.tempFilePath;  
                                //处理filepath拼接成新的下载地址进行下载  
                                var fileName = filePath.substring(filePath.lastIndexOf("/")+1);  
                                //截取名称前面的文件夹位置  
                                var fileName2 = fileName.substring(0,10);  
                                console.log('拼接地址');  
                                console.log(fileName);  
                                console.log(fileName2);  
                                var url2 = 'http://localhost:9999/' + fileName2 + '/'+ fileName;  
                                that.openDownLoad(url2);  

openDownLoad(url){
console.log(url);
uni.downloadFile({

                url:url,  
                 success: function(res){  

                     //打开新地址下载的文件  
                     uni.openDocument({  
                       filePath:res.tempFilePath,  
                       fileType:"pdf",  
                       success: function (res) {  
                            console.log(res);  
                            console.log('打开文档成功');  
                       },  
                       fail:function(err){  
                            console.log(err);  
                            console.log('打开文档失败');  
                       }   
                     });  
                 }  
             });  
        },
  • 天生DR

    可以让后端在命名时 规范统一一下

    2021-10-21 10:54

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