当前位置:   article > 正文

axios下载,多个下载并行

axios下载,多个下载并行

1.效果

2.主页面代码

  1. /**
  2. * @name DownLoad
  3. * @description 下载进度条
  4. */
  5. import React, { Component } from "react";
  6. // import axios from "axios";
  7. // import { isDev } from "@/utils/axios";
  8. import { Title } from "@/component";
  9. import { Button, Progress } from "antd";
  10. import { unitConver } from "@/utils";
  11. import { downFileCommon } from "@/utils/downFile";
  12. import styles from "./styles.module.less";
  13. const url1 = "/api-down-progress/uploads/soft/2108/2-210P2135407.rar";
  14. const url2 = "/api-down-progress/uploads/soft/2001/1-200121232528.zip";
  15. export default class DownLoad extends Component {
  16. state = {
  17. progress: 0,
  18. visible: true,
  19. size: 0,
  20. filename: ""
  21. };
  22. /**
  23. * 下载
  24. * @param {String} url 目标文件地址
  25. * @param {String} filename 想要保存的文件名称
  26. */
  27. //第1PPT地址-/api-down-progress:跨域代理=》http://ppt.1ppt.com
  28. proLine = (evt, num) => {
  29. console.log("evt===", evt);
  30. this.setState({ ["progress" + num]: parseInt((evt.loaded / evt.total) * 100), ["size" + num]: evt.total });
  31. };
  32. download = (url, num) => {
  33. downFileCommon(url, null, "get", null, null, this.proLine, num);
  34. };
  35. render() {
  36. const { visible, progress1, progress2, size1, size2, filename } = this.state;
  37. return (
  38. <div>
  39. <Title text="下载进度条" />
  40. <div className={styles.block}>
  41. {visible && (
  42. <div>
  43. <div>{filename}</div>
  44. <Progress percent={progress1 || 0} />
  45. {unitConver(size1 || 0)}
  46. </div>
  47. )}
  48. <Button onClick={() => this.download(url1, "1")}>下载1</Button>
  49. </div>
  50. <div className={styles.block}>
  51. {visible && (
  52. <div>
  53. <div>{filename}</div>
  54. <Progress percent={progress2 || 0} />
  55. {unitConver(size2 || 0)}
  56. </div>
  57. )}
  58. <Button onClick={() => this.download(url2, "2")}>下载2</Button>
  59. </div>
  60. </div>
  61. );
  62. }
  63. }

 3.封装的下载组件

  1. import axios from 'axios'
  2. import { message } from 'antd'
  3. export async function downFileCommon(url, data, type, token, filename, progress = () => { }, num) {
  4. axios({
  5. method: type,
  6. url,
  7. data,
  8. responseType: 'blob',
  9. headers: {
  10. // 身份验证,如果接口不需要,则传null
  11. Authorization: token,
  12. },
  13. onDownloadProgress: (evt) => {
  14. console.log("progressEvent===", evt);
  15. // 对原生进度事件的处理
  16. // 回调函数
  17. progress(evt, num)
  18. }
  19. })
  20. .then((res) => {
  21. // let type = res.headers['content-type'] //请求头中文件类型
  22. let Content = res.headers["Content-Disposition"] || res.headers["content-disposition"];
  23. let name = Content.split(";")[1].split("=")[1]; //请求头中文件名
  24. const blob = new Blob([res.data])
  25. if ('download' in document.createElement('a')) {
  26. // 非IE下载
  27. const elink = document.createElement('a')
  28. // 自定义文件名
  29. // elink.download = filename
  30. // 原文件名称
  31. elink.download = name;
  32. elink.style.display = 'none'
  33. elink.href = URL.createObjectURL(blob)
  34. document.body.appendChild(elink)
  35. elink.click()
  36. URL.revokeObjectURL(elink.href) // 释放URL 对象
  37. document.body.removeChild(elink)
  38. // console.log('下载第二步')
  39. } else {
  40. // IE10+下载
  41. navigator.msSaveBlob(blob, filename)
  42. }
  43. })
  44. .catch((err) => {
  45. message.error('下载失败')
  46. console.log('下载错误', err)
  47. })
  48. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/283542
推荐阅读
相关标签
  

闽ICP备14008679号