button class=‘down-img‘ bindtap=‘downImg‘ data-scr=‘{{imageUrl}}‘下载图片{{progress}}/button downImg: function (e) { var _this = this ; // 获取图片地址(http://www.playsort.cn/...) //var img = e.currentTarget.dataset.sr
<button class=‘down-img‘ bindtap=‘downImg‘ data-scr=‘{{imageUrl}}‘>下载图片{{progress}}</button>
downImg: function (e) {
var _this = this; // 获取图片地址(http://www.playsort.cn/...) //var img = e.currentTarget.dataset.src; //var img = ‘../pic/U1513P28T52D3414F872DT20081124143718.jpg?id=‘ + Math.floor(Math.random() * 100000);; var img = ‘http://i0.sinaimg.cn/ent/y/w/2008-11-24/U1513P28T52D3414F872DT20081124143718.jpg?id=‘ + Math.floor(Math.random() * 100000); // 下载监听进度 const downloadTask = wx.downloadFile({ url: img, success: function (res) { // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容 console.log(res) if (res.statusCode === 200) { // wx.saveImageToPhotosAlbum({ // filePath: res.tempFilePath, // success: function (res) { // wx.showToast({ // title: ‘保存图片成功!~‘, // }); // }, // fail: function (res) { // wx.showToast({ // title: ‘保存图片失败!~‘, // }); // } // }) console.log(‘code:‘ + res.statusCode ) }else{ console.log(‘code:‘ + res.statusCode ) } } }); downloadTask.onProgressUpdate((res) => { if (res.progress === 100) { this.setData({ progress: ‘100%‘ }); var st = getApp().globalData.st var et = new Date() console.log(et-st) console.log(410.45 * 1000 / (et - st)) }else if(res.progress === 1){ getApp().globalData.st = new Date() console.log(getApp().globalData.st); } else { this.setData({ progress: res.progress + ‘%‘ }); } }); }