download.ts 2.7 KB

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