You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

downloadfile.ts 2.6 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { request } from '@umijs/max';
  2. /** MimeType */
  3. export enum MimeType {
  4. XLSX = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  5. ZIP = 'application/zip',
  6. JSON = 'application/json',
  7. }
  8. /**
  9. * 解析blob响应内容并下载
  10. * @param res - blob响应内容
  11. * @param mimeType - MIME类型
  12. */
  13. export function resolveBlob(res: any, mimeType: string, specifiedFileName: string = 'file') {
  14. const aLink = document.createElement('a');
  15. const blob = new Blob([res.data], { type: mimeType });
  16. // 从response的headers中获取filename,
  17. // 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  18. const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
  19. // console.log(res);
  20. const contentDisposition = decodeURI(res.headers['content-disposition']);
  21. const result = patt.exec(contentDisposition);
  22. let fileName = result ? result[1] : specifiedFileName;
  23. fileName = fileName.replace(/"/g, '');
  24. aLink.style.display = 'none';
  25. aLink.href = URL.createObjectURL(blob);
  26. aLink.setAttribute('download', fileName); // 设置下载文件名称
  27. document.body.appendChild(aLink);
  28. aLink.click();
  29. URL.revokeObjectURL(aLink.href); // 清除引用
  30. document.body.removeChild(aLink);
  31. }
  32. /**
  33. * 下载 Zip 文件
  34. * @param url - url 地址
  35. * @param options - 请求参数
  36. */
  37. export function downLoadZip(url: string, params?: any) {
  38. request(url, {
  39. method: 'GET',
  40. params,
  41. responseType: 'blob',
  42. getResponse: true,
  43. }).then((res) => {
  44. resolveBlob(res, MimeType.ZIP);
  45. });
  46. }
  47. /**
  48. * 下载 XLSX 文件
  49. * @param url - url 地址
  50. * @param method - 请求方式
  51. * @param options - 请求选项
  52. */
  53. export async function downloadXlsx(
  54. url: string,
  55. method: string = 'GET',
  56. options?: Record<string, any>,
  57. ) {
  58. return request(url, {
  59. method: method,
  60. ...options,
  61. responseType: 'blob',
  62. getResponse: true,
  63. }).then((res) => {
  64. resolveBlob(res, MimeType.XLSX);
  65. });
  66. }
  67. /**
  68. * 下载文本文件
  69. * @param url - url 地址
  70. * @param type - mime-type
  71. * @param fileName - 文件名
  72. * @param method - 请求方法
  73. * @param options - 请求选项
  74. */
  75. export function downloadCommonFile(
  76. url: string,
  77. type: string,
  78. fileName: string = 'file',
  79. method: string = 'GET',
  80. options?: Record<string, any>,
  81. ) {
  82. request(url, {
  83. method: method,
  84. headers: {
  85. isToken: false,
  86. },
  87. skipValidating: true,
  88. ...options,
  89. responseType: 'blob',
  90. getResponse: true,
  91. }).then((res) => {
  92. resolveBlob(res, type, fileName);
  93. });
  94. }