download.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { openWindow } from '..';
  2. import { dataURLtoBlob, urlToBase64 } from './base64Conver';
  3. /**
  4. * Download online pictures
  5. * @param url
  6. * @param filename
  7. * @param mime
  8. * @param bom
  9. */
  10. export function downloadByOnlineUrl(url: string, filename: string, mime?: string, bom?: BlobPart) {
  11. urlToBase64(url).then((base64) => {
  12. downloadByBase64(base64, filename, mime, bom);
  13. });
  14. }
  15. /**
  16. * Download pictures based on base64
  17. * @param buf
  18. * @param filename
  19. * @param mime
  20. * @param bom
  21. */
  22. export function downloadByBase64(buf: string, filename: string, mime?: string, bom?: BlobPart) {
  23. const base64Buf = dataURLtoBlob(buf);
  24. downloadByData(base64Buf, filename, mime, bom);
  25. }
  26. /**
  27. * Download according to the background interface file stream
  28. * @param {*} data
  29. * @param {*} filename
  30. * @param {*} mime
  31. * @param {*} bom
  32. */
  33. export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
  34. const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
  35. const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
  36. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  37. window.navigator.msSaveBlob(blob, filename);
  38. } else {
  39. const blobURL = window.URL.createObjectURL(blob);
  40. const tempLink = document.createElement('a');
  41. tempLink.style.display = 'none';
  42. tempLink.href = blobURL;
  43. tempLink.setAttribute('download', filename);
  44. if (typeof tempLink.download === 'undefined') {
  45. tempLink.setAttribute('target', '_blank');
  46. }
  47. document.body.appendChild(tempLink);
  48. tempLink.click();
  49. document.body.removeChild(tempLink);
  50. window.URL.revokeObjectURL(blobURL);
  51. }
  52. }
  53. /**
  54. * Download file according to file address
  55. * @param {*} sUrl
  56. */
  57. export function downloadByUrl({
  58. url,
  59. target = '_blank',
  60. fileName,
  61. }: {
  62. url: string;
  63. target?: TargetContext;
  64. fileName?: string;
  65. }): boolean {
  66. const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  67. const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
  68. if (/(iP)/g.test(window.navigator.userAgent)) {
  69. console.error('Your browser does not support download!');
  70. return false;
  71. }
  72. if (isChrome || isSafari) {
  73. const link = document.createElement('a');
  74. link.href = url;
  75. link.target = target;
  76. if (link.download !== undefined) {
  77. link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
  78. }
  79. if (document.createEvent) {
  80. const e = document.createEvent('MouseEvents');
  81. e.initEvent('click', true, true);
  82. link.dispatchEvent(e);
  83. return true;
  84. }
  85. }
  86. if (url.indexOf('?') === -1) {
  87. url += '?download';
  88. }
  89. openWindow(url, { target });
  90. return true;
  91. }