downloadTaskbash
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 返回的兩個參數ui
tempFilePaththis
statusCodeurl
wx.downloadFile({
url: 'https://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 + '%'
})
}
})
},
複製代碼