downloadTask
UploadTask
downloadTask.onProgressUpdate((res) => {
console.log('下載進度', res.progress)
console.log('已經下載的數據長度', res.totalBytesWritten)
console.log('預期需要下載的數據總長度', res.totalBytesExpectedToWrite)
})
復制代碼
|
DownloadTask.offProgressUpdate(function callback) 復制代碼
DownloadTask.onHeadersReceived(function callback) 復制代碼
DownloadTask.offHeadersReceived(function callback) 復制代碼
DownloadTask.abort() 復制代碼
success 返回(hui)的兩(liang)個參數
wx.downloadFile({
url: '//example.com/audio/123',
header:'', //HTTP 請求的 Header,Header 中不能設置 Referer
filePath:'',//指定文件下載后存儲的路徑
success(res) {
// 只要服務器有響應數據,就會把響應內容寫入文件并進入 success 回調
//業務需要自行判斷是否下載到了想要的內容
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath
})
}
},
fail(err){
console.log(err)
},
complete(res){
console.log(res)
}
})
|
wx.saveImageToPhotosAlbum({
filePath:'',
success(res) {
},
fail(err){
},
complete(res){
}
})
|
/**
* [downloadPhoto 下載照片]
*/
downloadPhoto (e) {
let imgUrl = e.currentTarget.dataset.src
// 下載監聽進度
const downloadTask = wx.downloadFile({
url: imgUrl,
success: function (res) {
console.log(res)
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function (res) {
wx.showToast({
title: '保存圖片成功'
})
},
fail: function (res) {
wx.showToast({
title: '保存圖片失敗'
})
}
})
}
}
})
downloadTask.onProgressUpdate((res) => {
if (res.progress === 100) {
this.setData({
progress: ''
})
} else {
this.setData({
progress: res.progress + '%'
})
}
})
},
|