使用wx.downloadFile()API对gitee上的文件进行文件下载,下载的文件不全?
发布于 5 年前 作者 jing60 8082 次浏览 来自 官方Issues
使用wx.downloadFile()API进行文件下载,下载的文件不全,原文件有50K左右,下载下来只有一个文件壳子2K。使用java进行网络请求能下载成功。
下面是我用来进行下载的代码
 //1.请求固件下载地址  进行固件下载
    requestUpgradeUrl() {
        var that = this;
        wx.request({
            url'https://gitee.com/api/v5/repos/timetech/ZKXL-L312A/git/trees/master?recursive=1',
            method'GET',
            success(res) => {
                //下载成功的回调
                const list = res.data.tree;
                var tempList = [];
                for (let index = 0; index < list.length; index++) {
                    const element = list[index];
                    if (element.path.indexOf('ZL-L312A_HT4X') !== -1) {
                        tempList.push(element);
                    }
                }
                that.setData({
                    downloadFileNameList: tempList
                })
                that.DownLoadUrl(that.data.downloadFileNameList[0].path)
            },
            fail(res) => {
                //下载失败的回调
                console.log(res.data.tree)

            }
        })
    },

    //2.请求至固件下载地址后  进行下载
    DownLoadUrl(path) {
        // const path = downloadFileNameList[0].url;
        const url = "https://gitee.com/api/v5/repos/timetech/ZKXL-L312A/contents/" + path + "?ref=master";
        console.log(path)
        wx.request({
            url: url,
            method"GET",
            success(res) => {
                console.log(res);
                const download_url = res.data.download_url;
                this.downloadFile(download_url, path);
            },
            fail(res) => {

            }
        })
    },

    //下载固件文件
    downloadFile(download_url, path) {
        const targetPath = wx.env.USER_DATA_PATH;
        console.log("targetPath=" + targetPath);
        console.log("download_url=" + download_url)
        console.log(path)
        wx.downloadFile({
            url: download_url,
            success(result) => {
                console.log(result)
                wx.saveFile({
                    tempFilePath: result.tempFilePath,
                    filePath: targetPath + "/" + path,
                    success(res) => {
                        console.log(res)
                        //固件存储位置 
                        const savedFilePath = res.savedFilePath
                        console.log(savedFilePath);
                        wx.showToast({
                            title'下载成功',
                            icon'success',
                        })
                        //解压文件为固件更新作准备
                        // this.unzip(savedFilePath);
                    },
                    fail(res) => {
                        console.log("下载失败:" + res);
                    }
                })

            },
            fail(res) => {

            }
        })
    },

1 回复

校验环境了吧

回到顶部