downloadTask
UploadTask
downloadTask.onProgressUpdate((res) => {
console.log('下載進(jìn)度', res.progress)
console.log('已經(jīng)下載的數(shù)據(jù)長(zhǎng)度', res.totalBytesWritten)
console.log('預(yù)期需要下載的數(shù)據(jù)總長(zhǎng)度', res.totalBytesExpectedToWrite)
})
復(fù)制代碼
|
DownloadTask.offProgressUpdate(function callback) 復(fù)制代碼
DownloadTask.onHeadersReceived(function callback) 復(fù)制代碼
DownloadTask.offHeadersReceived(function callback) 復(fù)制代碼
DownloadTask.abort() 復(fù)制代碼
success 返回的兩個(gè)參數(shù)
wx.downloadFile({
url: 'https://example.com/audio/123',
header:'', //HTTP 請(qǐng)求的 Header,Header 中不能設(shè)置 Referer
filePath:'',//指定文件下載后存儲(chǔ)的路徑
success(res) {
// 只要服務(wù)器有響應(yīng)數(shù)據(jù),就會(huì)把響應(yīng)內(nèi)容寫入文件并進(jìn)入 success 回調(diào)
//業(yè)務(wù)需要自行判斷是否下載到了想要的內(nèi)容
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
// 下載監(jiān)聽進(jìn)度
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 + '%'
})
}
})
},
|