DownloadTask.onProgressUpdate

解释:监听下载进度变化

方法参数

Function callback

返回参数说明

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

示例

在开发者工具中预览效果

扫码体验

DownloadTask.onProgressUpdate - 图1请使用百度APP扫码

图片示例

DownloadTask.onProgressUpdate - 图2

DownloadTask.onProgressUpdate - 图3

DownloadTask.onProgressUpdate - 图4

代码示例

  • 在 js 文件中
  1. Page({
  2. data: {
  3. alreadyData: 0,
  4. targetData: 0
  5. },
  6. downloadFile() {
  7. const downloadTask = swan.downloadFile({
  8. url: 'https://smartprogram.baidu.com/docs/img/file-simple.pdf',
  9. header: {
  10. 'content-type': 'application/json'
  11. },
  12. success: res => {
  13. console.log(res.tempFilePath);
  14. },
  15. fail: err => {
  16. console.log('错误码:' + err.errCode);
  17. console.log('错误信息:' + err.errMsg);
  18. },
  19. complete: () => {
  20. }
  21. });
  22. downloadTask.onProgressUpdate(res => {
  23. console.log('上传进度', res.progress);
  24. this.setData({
  25. 'progress': res.progress,
  26. 'alreadyData': res.totalBytesWritten,
  27. 'targetData': res.totalBytesExpectedToWrite
  28. })
  29. console.log('已经上传的数据长度', res.totalBytesWritten);
  30. console.log('预期需要上传的数据总长度', res.totalBytesExpectedToWrite);
  31. });
  32. }
  33. });