天机不可泄露
天机不可泄露
  • 发布:2020-05-14 22:30
  • 更新:2020-05-23 10:21
  • 阅读:3540

为什么使用uni.downloadFile()下载文件1次或多次后再下载就没反应了

分类:uni-app

在使用这个方法下载文件下载1次或多次后再下载就没反应了,在uni.downloadFile()中console.log()就没有动静,会直接跳过这个方法

/**  打开文件  
 *  fileUrl 文件路径  
 *  fileInfo 文件信息  
 * */  
function openFile(fileUrl, fileInfo = []) {  
    // 判断是否为网络文件  
    if (fileUrl.indexOf('http://') == -1 && fileUrl.indexOf('https://') == -1) { //本地文件直接打开  
        uni.openDocument({  
            filePath: fileUrl,  
            success: function(res) {  
                console.log('打开文档成功', res);  
            },  
            fail: function(err) {  
                console.log('打开文档失败', err);  
                return uni.showToast({  
                    title: '文档打开失败!',  
                    icon: 'none'  
                })  
            }  
        });  
    } else { //网络文件需下载  
        console.log('2', fileUrl)  
        let userName = localStorage.getItem('user_name');  
        let list = localStorage.getItem(userName + '-downloadList');  
        list = list && list != undefined ? JSON.parse(list) : [];  
        if (list.length) {  
            for (let i = 0; i < list.length; i++) {  
                let item = list[i];  
                if (item.networkFileUrl == fileUrl) {  
                    console.log('文件已下载')  
                    return openFile(item.offlineFileUrl);  
                }  
            }  
        }  
        uni.downloadFile({  
            url: encodeURI(fileUrl),  
            success: function(res) {  
                console.log('下载文件', res)  
                if (res.statusCode === 200) {  
                    uni.saveFile({  
                        tempFilePath: res.tempFilePath,  
                        success: function(res2) {  
                            let path = res2.savedFilePath; //永久路径  
                            /*let fileItem = {  
                                fileName: fileInfo.fileName ? fileInfo.fileName : fileInfo.reportName,  
                                networkFileUrl: fileInfo.fileUrl,  
                                offlineFileUrl: path  
                            };  
                            saveFileInfo(fileItem);*/  
                            console.log('保存文件', res2)  
                            uni.openDocument({  
                                filePath: path,  
                                success: function(res) {  
                                    console.log('打开文档成功', res);  
                                },  
                                fail: function(err) {  
                                    console.log('打开文档失败', err)  
                                    return uni.showToast({  
                                        title: '文档打开失败!',  
                                        icon: 'none'  
                                    })  
                                }  
                            });  
                        }  
                    })  
                } else {  
                    removeDownloadFile(res.tempFilePath)  
                    return uni.showToast({  
                        title: '文件下载失败',  
                        icon: 'none'  
                    });  
                }  
            },  
            fail: function(err) {  
                console.log(err)  
                return uni.showToast({  
                    title: '文件下载失败',  
                    icon: 'none'  
                });  
            }  
        })  
            console.log(‘-------------------’)  
    }  
}
2020-05-14 22:30 负责人:无 分享
已邀请:
7***@qq.com

7***@qq.com

同问,不知道楼主解决了吗

2***@qq.com

2***@qq.com

我遇到过这个问题,解决了 ;第一次打开肯定是没有问题了,第二次 uni.downloadFile 生成的临时文件不存在的,生成临时文件名后多了(1),只需要把返回的 res.tempFilePath 中的 (1)替换掉就可以了,当然需要写个通用的方法就可以了,比如:
that.openfilePath =res.tempFilePath;
var sidx=that.openfilePath.lastIndexOf('(');
var eidx=that.openfilePath.lastIndexOf(')');
if(sidx>-1 && eidx>-1)
{
var restr=that.openfilePath.substr(sidx,eidx-sidx+1);
that.openfilePath = that.openfilePath.replace(restr, '');
}

  • 7***@qq.com

    请问有没有更详细一点的代码片段

    2020-06-08 14:45

  • 2***@qq.com

    回复 7***@qq.com: 就这个代码处理一下获取临时文件的名称,把(1)、(2)、这样循环去掉之后,还是uni-app 自带的 打开文档,我这边已经上线正式使用了,ios和安卓都正常使用了

    2020-06-12 11:31

  • y***@qq.com

    回复 2***@qq.com: 是第一次下载完成后处理吗

    2020-06-12 18:10

  • y***@qq.com

    回复 2***@qq.com: 我已经解决了

    2020-06-12 18:32

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