Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

DownloadTask

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

方法

DownloadTask.abort()

中断下载任务

DownloadTask.onProgressUpdate(Function callback)

监听下载进度变化事件

参数
参数类型默认值必填说明
callbackFunction是进度回调

DownloadTask.onProgressUpdate.callback 回调参数

属性类型说明
progressNumber下载进度百分比
totalBytesWrittenNumber已经下载的数据长度,单位 Bytes
totalBytesExpectedToWriteNumber预期需要下载的数据总长度,单位 Bytes

DownloadTask.offProgressUpdate(Function callback)

参数
参数类型默认值必填说明
callbackFunction是需要取消的监听函数

取消监听下载进度变化事件

示例代码:

const downloadTask = mc.downloadFile({
  url: 'http://test.com/',
  success (res) {
    console.log(res.tempFilePath)
  }
})

downloadTask.onProgressUpdate((res) => {
  console.log('下载进度', res.progress)
  console.log('已经下载的数据长度', res.totalBytesWritten)
  console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
})

downloadTask.abort() // 取消下载任务