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;
});

最终效果如下:

相关推荐
大菠萝学姐12 分钟前
基于springboot的旅游攻略网站设计与实现
前端·javascript·vue.js·spring boot·后端·spring·旅游
心随雨下24 分钟前
TypeScript中extends与implements的区别
前端·javascript·typescript
Little丶Seven25 分钟前
使用adb获取安卓模拟器日志
android·unity·adb·个人开发
凉栀お_27 分钟前
MySQL第五次作业(触发器,存储过程)
android·mysql·adb
caleb_52030 分钟前
vue cli的介绍
前端·javascript·vue.js
Swift社区31 分钟前
如何监测 Vue + GeoScene 项目中浏览器内存变化并优化性能
前端·javascript·vue.js
limingade31 分钟前
ADB点击实战-做一个自动点广告播放领金币的脚本app(中)
android·adb·智能手机·ocr识别手机广告·ocr识别手机屏幕·adb自动关闭广告
WYiQIU34 分钟前
大厂前端岗重复率极高的场景面试原题解析
前端·javascript·vue.js·react.js·面试·状态模式
zbhbbedp282793cl39 分钟前
unique_ptr和shared_ptr有何区别?
java·开发语言·jvm
珹洺1 小时前
Java-Spring入门指南(二十九)Android交互核心:按钮点击事件与Activity跳转实战
android·java·spring