download.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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({ url, target = '_blank', fileName }: { url: string; target?: TargetContext; fileName?: string }): boolean {
  58. const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  59. const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
  60. if (/(iP)/g.test(window.navigator.userAgent)) {
  61. console.error('Your browser does not support download!');
  62. return false;
  63. }
  64. if (isChrome || isSafari) {
  65. const link = document.createElement('a');
  66. link.href = url;
  67. link.target = target;
  68. if (link.download !== undefined) {
  69. link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
  70. }
  71. if (document.createEvent) {
  72. const e = document.createEvent('MouseEvents');
  73. e.initEvent('click', true, true);
  74. link.dispatchEvent(e);
  75. return true;
  76. }
  77. }
  78. if (url.indexOf('?') === -1) {
  79. url += '?download';
  80. }
  81. openWindow(url, { target });
  82. return true;
  83. }