download.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { AxiosResponseHeaders, RawAxiosResponseHeaders } from 'axios';
  2. import { dataURLtoBlob, urlToBase64 } from './base64Conver';
  3. import * as url from "node:url";
  4. /**
  5. * Download online pictures
  6. * @param url
  7. * @param filename
  8. * @param mime
  9. * @param bom
  10. */
  11. export function downloadByOnlineUrl(url: string, filename: string, mime?: string, bom?: BlobPart) {
  12. urlToBase64(url).then((base64) => {
  13. downloadByBase64(base64, filename, mime, bom);
  14. });
  15. }
  16. /**
  17. * Download pictures based on base64
  18. * @param buf
  19. * @param filename
  20. * @param mime
  21. * @param bom
  22. */
  23. export function downloadByBase64(buf: string, filename: string, mime?: string, bom?: BlobPart) {
  24. const base64Buf = dataURLtoBlob(buf);
  25. downloadByData(base64Buf, filename, mime, bom);
  26. }
  27. /**
  28. * Download according to the background interface file stream
  29. * @param {*} data
  30. * @param {*} filename
  31. * @param {*} mime
  32. * @param {*} bom
  33. */
  34. export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
  35. const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
  36. const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
  37. const blobURL = window.URL.createObjectURL(blob);
  38. const tempLink = document.createElement('a');
  39. tempLink.style.display = 'none';
  40. tempLink.href = blobURL;
  41. tempLink.setAttribute('download', filename);
  42. if (typeof tempLink.download === 'undefined') {
  43. tempLink.setAttribute('target', '_blank');
  44. }
  45. document.body.appendChild(tempLink);
  46. tempLink.click();
  47. document.body.removeChild(tempLink);
  48. window.URL.revokeObjectURL(blobURL);
  49. }
  50. /**
  51. * Download file according to file address
  52. * @param {*} sUrl
  53. */
  54. export function downloadByUrl({ url, target = '_blank', fileName }: { url: string; target?: string; fileName?: string }): boolean {
  55. const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  56. const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
  57. if (/(iP)/g.test(window.navigator.userAgent)) {
  58. console.error('Your browser does not support download!');
  59. return false;
  60. }
  61. if (isChrome || isSafari) {
  62. const link = document.createElement('a');
  63. link.href = url;
  64. link.target = target;
  65. if (link.download !== undefined) {
  66. link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
  67. }
  68. if (document.createEvent) {
  69. const e = document.createEvent('MouseEvents');
  70. e.initEvent('click', true, true);
  71. link.dispatchEvent(e);
  72. return true;
  73. }
  74. }
  75. if (url.indexOf('?') === -1) {
  76. url += '?download';
  77. }
  78. openWindow(url, { target });
  79. return true;
  80. }
  81. export function openWindow(url: string, opt?: { target?: string; noopener?: boolean; noreferrer?: boolean }) {
  82. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  83. const feature: string[] = [];
  84. noopener && feature.push('noopener=yes');
  85. noreferrer && feature.push('noreferrer=yes');
  86. window.open(url, target, feature.join(','));
  87. }
  88. export function getFileName(headers: RawAxiosResponseHeaders | AxiosResponseHeaders) {
  89. var fileName = headers['content-disposition'].split(';')[1].split('filename=')[1];
  90. var fileNameUnicode = headers['content-disposition'].split('filename*=')[1];
  91. if (fileName?.includes("%")) fileName = decodeURIComponent(fileName);
  92. if (fileNameUnicode) {
  93. //当存在 filename* 时,取filename* 并进行解码(为了解决中文乱码问题)
  94. fileName = decodeURIComponent(fileNameUnicode.split("''")[1]);
  95. }
  96. return fileName;
  97. }
  98. /**
  99. * 文件流下载
  100. * @param res
  101. * @param fileName 文件名
  102. */
  103. export function downloadStreamFile(res: any, fileName: string | undefined = undefined) {
  104. const contentType = res.headers['content-type'];
  105. fileName = fileName || getFileName(res.headers);
  106. const blob = res.data instanceof Blob ? res.data : new Blob([res.data], { type: contentType });
  107. downloadByUrl({ url: window.URL.createObjectURL(blob), fileName });
  108. }