前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Axios下载文件示例

Axios下载文件示例

作者头像
徐建国
发布2024-06-19 20:26:56
1140
发布2024-06-19 20:26:56
举报
文章被收录于专栏:个人路线个人路线

Axios下载文件示例

设置下载路径filePath(默认在'internal://cache/'路径下)。

关于filePath filePath:'workspace/test.txt':默认路径下创建workspace路径,并将文件存储在workspace路径下。 filePath:'test.txt':将文件存储在默认路径下。 filePath:'workspace/':默认路径下创建workspace路径,并将文件存储在workspace路径下。

完整源码

代码语言:javascript
复制
import axios, { AxiosError, AxiosProgressEvent, AxiosResponse } from '@ohos/axios';
import { promptAction } from '@kit.ArkUI';
import fs from '@ohos.file.fs';

@Entry
@Component
struct SecondPage {
  @State message: string = 'Hello World';
  @State downloadProgress: number = 0
  @State status: string | number = ''
  @State startTime: number = 0;
  @State endTime: number = 0;

  aboutToAppear(): void {

    this.downLoadFile()
  }


  downLoadFile() {
    this.startTime = new Date().getTime();
    let filePath = getContext(this).cacheDir + '/git.pdf'
    // 下载。如果文件已存在,则先删除文件。
    try {
      fs.accessSync(filePath);
      fs.unlinkSync(filePath);
    } catch (err) {
    }

    axios({
      url: 'https://www.gjtool.cn/pdfh5/git.pdf',
      method: 'get',
      context: getContext(this),
      filePath: filePath,
      onDownloadProgress: (progressEvent: AxiosProgressEvent): void => {
        promptAction.showToast({
          message: "下载进度"
        })

        this.message = JSON.stringify(progressEvent.progress)
        console.info("hhhhhhhh" + JSON.stringify(progressEvent))
        this.downloadProgress = progressEvent && progressEvent.loaded && progressEvent.total ?
        Math.ceil(progressEvent.loaded / progressEvent.total * 100) : 0;
        // console.info("progress: " + progressEvent && progressEvent.loaded && progressEvent.total ? Math.ceil(progressEvent.loaded / progressEvent.total * 100) : 0)
      }
    }).then((res: AxiosResponse<string>) => {
      this.status = res ? res.status : '';
      this.message = res ? JSON.stringify(res.data) : '';
      this.endTime = new Date().getTime();
    }).catch((err: AxiosError) => {
      this.status = '';
      this.message = err.message;
      this.endTime = new Date().getTime();
    })
  }

  build() {
    Column() {
      Text(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)

      Progress({ value: this.downloadProgress, type: ProgressType.Linear })
        .color('#009BE8').width('100%')
        .margin({ top: 8, right: 10 })
        .style({ strokeWidth: 10 })
    }
    .height('100%')
    .width('100%')
  }
}

参考

axios[1]

参考资料

[1]

axios: https://gitee.com/openharmony-sig/ohos_axios#axios

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大前端之旅 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Axios下载文件示例
  • 完整源码
  • 参考
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档