DownloadTask

支持版本 >= 1.4.0

一个可以监听下载进度变化事件,以及取消下载任务的对象

方法

DownloadTask.abort()

中断下载任务

DownloadTask.onProgressUpdate(function callback)

监听下载进度变化事件

示例代码

  1. const downloadTask = wx.downloadFile({
  2. url: 'http://example.com/audio/123', //仅为示例,并非真实的资源
  3. success (res) {
  4. wx.playVoice({
  5. filePath: res.tempFilePath
  6. })
  7. }
  8. })
  9. downloadTask.onProgressUpdate((res) => {
  10. console.log('下载进度', res.progress)
  11. console.log('已经下载的数据长度', res.totalBytesWritten)
  12. console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
  13. })
  14. downloadTask.abort() // 取消下载任务

原文: https://developers.weixin.qq.com/minigame/dev/api/network/download/DownloadTask.html