axios 下载大文件时,展示下载进度的组件封装——js技能提升

之前面试的时候,有遇到一个问题:就是下载大文件的时候,如何得知下载进度,当时的回复是没有处理过。。。

现在想到了。axios中本身就有一个下载进度的方法,可以直接拿来使用。

下面记录一下处理步骤:

参考链接:https://blog.csdn.net/yyh123456hhh/article/details/131637151

解决步骤1:给封装好的axios方法中添加onDownloadProgress

这个方法就是监听接口进度的方法了,可以作为入参进行处理。

解决步骤2:在使用request时,写入onDownloadProgress

js 复制代码
export async function exportPageList(params, config, downloadProgress) {
  return request(
    `/api/quality-service/FeedReasons/export-feedreason-datas`,
    METHOD.GET,
    params,
    config,//请求头或者文档格式设置等
    downloadProgress//接口请求进度
  );
}

解决步骤3:具体使用方法

html部分:

js 复制代码
<a-modal
    title="导出"
    :footer="null"
    :visible="visible"
    :width="500"
    :closable="false"
  >
    <div class="download-progress">
      <a-progress :percent="percent" />
      <p>正在导出...</p>
    </div>
</a-modal>

需要传入的参数:visible percent

是否展示弹窗和进度条占比

js 复制代码
exportPageList(params,{responseType: 'blob'},
	(progress) => {
	  this.visible = true;
	  this.percent = (progress.loaded / progress.total) * 100;
	  if (this.percent >= 100) {
	    setTimeout(() => {
	      this.visible = false;
	    }, 200);
	  }
	}
)
.then((res) => {
  let blobUrl = window.URL.createObjectURL(res);
  let link = document.createElement('a');
  link.style.display = 'none';
  link.download = `报废原因配置_${moment().format('YYYY/MM/DD')}.xlsx`;
  link.href = blobUrl;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
})
.finally(() => {
  this.spinning = false;
});

最终效果如下:

相关推荐
神仙别闹9 分钟前
基于SpringMVC+Spring+MyBatis开发的个人博客网站
java·spring·mybatis
华仔啊30 分钟前
MyBatis-Plus 不只是简化CRUD!资深架构师总结的15个高阶用法
java·后端·mybatis
不爱吃糖的程序媛33 分钟前
Electron 智能文件分析器开发实战适配鸿蒙
前端·javascript·electron
Han.miracle1 小时前
Java EE --JUC
java·线程池·原子类·callable·semaphore·reentrantlcok
那我掉的头发算什么1 小时前
【javaEE】多线程——线程安全初阶☆☆☆
java·jvm·安全·java-ee·intellij-idea
flashlight_hi1 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表
Java追光着1 小时前
React Native 自建 JS Bundle OTA 更新系统:从零到一的完整实现与踩坑记录
javascript·react native·react.js
努力往上爬de蜗牛1 小时前
react native 运行问题和调试 --持续更新
javascript·react native·react.js
yzp-1 小时前
Zookeeper 笔记
java·分布式·zookeeper
蜡笔大新7981 小时前
IDEA中的异常
java·ide·intellij-idea