download.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // import { openWindow } from '..';
  2. import { dataURLtoBlob, urlToBase64 } from './base64Conver';
  3. import { defHttp } from '@jeesite/core/utils/http/axios';
  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 nav = window.navigator as any;
  38. if (typeof nav.msSaveBlob !== 'undefined') {
  39. nav.msSaveBlob(blob, filename);
  40. } else {
  41. const blobURL = window.URL.createObjectURL(blob);
  42. const tempLink = document.createElement('a');
  43. tempLink.style.display = 'none';
  44. tempLink.href = blobURL;
  45. tempLink.setAttribute('download', filename);
  46. if (typeof tempLink.download === 'undefined') {
  47. tempLink.setAttribute('target', '_blank');
  48. }
  49. document.body.appendChild(tempLink);
  50. tempLink.click();
  51. document.body.removeChild(tempLink);
  52. window.URL.revokeObjectURL(blobURL);
  53. }
  54. }
  55. /**
  56. * Download file according to file address
  57. * @param {*} sUrl
  58. */
  59. export async function downloadByUrl({
  60. url,
  61. params,
  62. data,
  63. // target = '_self',
  64. fileName,
  65. }: {
  66. url: string;
  67. params?: any;
  68. data?: any;
  69. // target?: TargetContext;
  70. fileName?: string;
  71. }): Promise<boolean> {
  72. const res = await defHttp.post(
  73. { url, params, data, responseType: 'blob' },
  74. { isReturnNativeResponse: true, joinPrefix: false },
  75. );
  76. let name = res.headers['content-disposition'];
  77. name = name && name.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
  78. name = name && name.length >= 1 && name[1].replace("utf-8'zh_cn'", '');
  79. name = (name && decodeURIComponent(name)) || fileName || 'jeesite';
  80. downloadByData(res.data, name);
  81. // axios({
  82. // url: url,
  83. // method: 'post',
  84. // data: data,
  85. // responseType: 'blob',
  86. // })
  87. // .then((response) => {
  88. // let name = response.headers['content-disposition'];
  89. // name = name && name.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
  90. // name = name && name.length >= 1 && name[1].replace("utf-8'zh_cn'", '');
  91. // name = name && (decodeURIComponent(name) || fileName || 'jeesite');
  92. // downloadByData(response.data, name);
  93. // })
  94. // .catch((error) => {
  95. // console.error(error);
  96. // });
  97. // const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  98. // const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
  99. // if (/(iP)/g.test(window.navigator.userAgent)) {
  100. // console.error('Your browser does not support download!');
  101. // return false;
  102. // }
  103. // if (isChrome || isSafari) {
  104. // const link = document.createElement('a');
  105. // link.href = url;
  106. // link.target = target;
  107. // if (link.download !== undefined) {
  108. // link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
  109. // }
  110. // if (document.createEvent) {
  111. // const e = document.createEvent('MouseEvents');
  112. // e.initEvent('click', true, true);
  113. // link.dispatchEvent(e);
  114. // return true;
  115. // }
  116. // }
  117. // if (url.indexOf('?') === -1) {
  118. // url += '?download';
  119. // }
  120. // openWindow(url, { target });
  121. return true;
  122. }