HarmonyOS axios不回调下载进度

axios.request设置了onDownloadProgress但不回调。

HarmonyOS
2025-01-10 08:20:15
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

示例参考:

import axios, { AxiosError, AxiosProgressEvent, AxiosResponse } from '@ohos/axios';
import fs from '@ohos.file.fs';
import { common } from '@kit.AbilityKit';
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State downloadProgress: number = 0

  build() {
    RelativeContainer() {
      AxiosDownloadProgress()
    }
    .height('100%')
    .width('100%')
  }
}

@Component
export struct AxiosDownloadProgress {
  @State context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
  @State total: number = 0
  @State loaded: number = 0

  build() {
    Row() {
      Button("清理下载数据").onClick(() => {
        try {
          fs.unlinkSync(`${this.context.cacheDir}/1.jpg`)
        } catch (e) {
          promptAction.showToast({
            message: "文件不存在"
          })
        }
        promptAction.showToast({
          message: "清理成功"
        })
      })

      Button("下载").onClick(() => {
        try {
          axios.request({
            url: 'xxxx',
            method: 'GET',
            onDownloadProgress: (progressEvent) => {
              progressEvent.total && (this.loaded = progressEvent.loaded / progressEvent.total * 100)
              console.log(this.loaded.toString())
              if (this.loaded === 100) {
                promptAction.showToast({
                  message: "下载完成"
                })
              }
            },
            filePath: this.context.cacheDir + '/1.jpg'
          });
        } catch (e) {
          console.log(e)
        }
      })
      Progress({ value: this.loaded, total: 100, type: ProgressType.Ring })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
分享
微博
QQ
微信
回复
2025-01-10 11:56:41


相关问题
HarmonyOS axios下载文件问题
518浏览 • 1回复 待解决
HarmonyOS 如何监听下载文件的进度
694浏览 • 1回复 待解决
HarmonyOS页面onPageShow生命周期不回
1461浏览 • 1回复 待解决
TextInput的onBlur方法不回
1687浏览 • 1回复 待解决
HarmonyOS axios 如何代理
567浏览 • 1回复 待解决
HarmonyOS axios用法咨询
634浏览 • 1回复 待解决
HarmonyOS axios请求问题
460浏览 • 1回复 待解决
seata 在 OpenFeign fallback 下不回滚事务
4220浏览 • 2回复 待解决
HarmonyOS 进度条样式
434浏览 • 1回复 待解决