使用wx.downloadFile()API进行文件下载,下载的文件不全,原文件有50K左右,下载下来只有一个文件壳子2K。使用java进行网络请求能下载成功。
下面是我用来进行下载的代码
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)
}
})
},
DownLoadUrl(path) {
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',
})
},
fail: (res) => {
console.log("下载失败:" + res);
}
})
},
fail: (res) => {
}
})
},