8***@qq.com
8***@qq.com
  • 发布:2019-03-28 09:59
  • 更新:2019-11-29 11:19
  • 阅读:4424

下载文件成功后,使用uni.saveFile时,文件名被重命名了

分类:uni-app

使用uni.saveFile时,文件名被修改了,如"filePath":"_doc/uniapp_save/1553737301613.xlsx","createTime":1553737301,"size":8560
请问如何设置为跟临时文件同名,还有怎么指定下载目录,目前下载成功后,手机需要重启后才能在电脑上找到(我是这样找的:计算机\LDN-AL00\内部存储\Android\data\io.dcloud.HBuilder\apps\HBuilder\doc\uniapp_save)

2019-03-28 09:59 负责人:无 分享
已邀请:
z***@163.com

z***@163.com

您好,请问解决了吗?我也遇到这样的问题。

  • 8***@qq.com (作者)

    没有,你解决了吗,如果可以,求告知,谢谢。

    2019-10-29 11:46

HawkLu92

HawkLu92 - GoodGoodCoding,DayDayNoBug

我也是遇到这个问题,本来想用H5+ plus.io接口拷贝到指定的文件夹,但是折腾栏半天还是搞不定。
因为不管是downloadFile下载下来的临时文件还是saveFile保存的文件,都只能通过当前的APP去操作。
后来想到一种曲线救国的办法,就是下载的时候存入一个storage标识,key可以用原来的文件名,value就用存储后的名字。这样就可以从storage中拿取对应的文件栏,但是注意storage在不同的端有区别。

下面贴一些我自己写的APP更新业务,写的很烂,将就看看。
这个是主逻辑

/**  
     * @description 更新APP  
     * @param {Object} res 接口传递过来的更新信息  
     */  
    updateApp(updateJson) {  
        let _this = this;  
        _this.getLocalUpdatePackageByVersion(updateJson.newVersion).then(storageUrl => { //用接口获取的版本号检查storage缓存  
            if (storageUrl) { //有storage缓存  
                console.log("有storage缓存" + storageUrl);  
                _this.checkSavedUpdatePackage(storageUrl).then(filePath => { //通过storage缓存中的文件路径查看该文件存不存在  
                    if (filePath) { //存在走安装流程  
                        console.log("开始安装");  
                        _this.installUpdatePackage(filePath);  
                    } else { //不存在先下载    
                        _this.downloadFile(updateJson.androidUrl).then(tempFilePath=>{  //下载文件  
                            _this.saveFile(updateJson.newVersion,tempFilePath).then(savedFilePath=>{    //存储下载的临时文件  
                                _this.installUpdatePackage(savedFilePath);  
                            })  
                        });  
                    }  
                })  
            } else { //没有  
                _this.downloadFile(updateJson.androidUrl).then(tempFilePath=>{  //下载文件  
                    _this.saveFile(updateJson.newVersion,tempFilePath).then(savedFilePath=>{    //存储下载的临时文件  
                        _this.installUpdatePackage(savedFilePath);  
                    })  
                });  
            }  
        })  
    },  

存储文件业务

/**  
     * @description 存储下载的文件  
     * @param {Object} tempFile  下载后暂存的文件路径,暂存文件在APP重启后会消失  
     * @param {String} version 安装包的版本号   
     */  
    saveFile(version,tempFilePath) {  
        return new Promise((resolve, reject) => {  
            uni.saveFile({  
                tempFilePath: tempFilePath,  
                success: function(res) {  
                    uni.setStorage({  
                        key: version,  
                        data: res.savedFilePath,  
                        success() {  
                            console.log("存储文件成功,存储地址:" + res.savedFilePath);  
                            resolve(res);  
                        }  
                    })  
                },  
                fail: (err) => {  
                    console.log("存储文件失败");  
                }  
            });  
        })  

    },  
这不是我没有

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