download.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const blobURL = window.URL.createObjectURL(blob);
  37. const tempLink = document.createElement('a');
  38. tempLink.style.display = 'none';
  39. tempLink.href = blobURL;
  40. tempLink.setAttribute('download', filename);
  41. if (typeof tempLink.download === 'undefined') {
  42. tempLink.setAttribute('target', '_blank');
  43. }
  44. document.body.appendChild(tempLink);
  45. tempLink.click();
  46. document.body.removeChild(tempLink);
  47. window.URL.revokeObjectURL(blobURL);
  48. }
  49. /**
  50. * Download file according to file address
  51. * @param {*} sUrl
  52. */
  53. export function downloadByUrl({
  54. url,
  55. target = '_blank',
  56. fileName,
  57. }: {
  58. url: string;
  59. target?: TargetContext;
  60. fileName?: string;
  61. }): boolean {
  62. const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  63. const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
  64. if (/(iP)/g.test(window.navigator.userAgent)) {
  65. console.error('Your browser does not support download!');
  66. return false;
  67. }
  68. if (isChrome || isSafari) {
  69. const link = document.createElement('a');
  70. link.href = url;
  71. link.target = target;
  72. if (link.download !== undefined) {
  73. link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
  74. }
  75. if (document.createEvent) {
  76. const e = document.createEvent('MouseEvents');
  77. e.initEvent('click', true, true);
  78. link.dispatchEvent(e);
  79. return true;
  80. }
  81. }
  82. if (url.indexOf('?') === -1) {
  83. url += '?download';
  84. }
  85. openWindow(url, { target });
  86. return true;
  87. }