微信小程序之wx.downloadFile

down_file: function() {
var b;
var _this = this;
for (let x = 0, y = 16; x < y; x++) {
for (let i = 0, j = 20; i < j; i++) {
if (((x == 4) || (x == 5)) & j > 8) {
break;
}
 cc为拼起来的字符串
var cc = "this.data." + this.data.allArray[i];
 
switch (x) {
case 0:
b = this.data.eye111A[i];
// 这里原先有16个的
break;
}
var Array = [, , , , , , , , , , , , , , , , , , , ];
const downloadTask = wx.downloadFile({
url: b,
success: function(res) {
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
console.log(res)
if (res.statusCode === 200) {
_this.setData({
img_l: res.tempFilePath,
})
Array[i] = res.tempFilePath;

// let chose = "eye111B"
let chose = _this.data.allArray[i];
var bb = Array;
if (Array.length == 20 && ((x == 4 || x == 5) && i == 8)) {
_this.setData({
[chose]: bb,
})
console.log("成功换皮");
}
}
},
fail: function(err) {
console.log(err)
}
});
}
}
},
 
 
这其中的
eye111B: ["https://xxxxxx.xyz/images/A/eye111/B/a32.png", ],
1.地址要用域名 不能用ip地址;
2.直接在this.setData里面push是不成功的所以新建了一个固定长度的数组然后for循环填充数组。
3.这里面因为转换速度不定,导致前面的地址还没下完,但是后面的地址已经替换所以,替换的时候要固定位置。
 
 
 
wx.downloadFile会返回一个地址给你当你在小程序里面有很多图片导致只能使用网络图片的时候,就可以依靠这样的方法来替换图片地址,而不会一次次的重复加载。
原文地址:https://www.cnblogs.com/thy95/p/10103444.html