DownloadURL.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* *
  2. *
  3. * (c) 2015-2021 Oystein Moseng
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * Mixin for downloading content in the browser
  10. *
  11. * */
  12. 'use strict';
  13. import Highcharts from '../Core/Globals.js';
  14. var isSafari = Highcharts.isSafari;
  15. var win = Highcharts.win, doc = win.document, domurl = win.URL || win.webkitURL || win;
  16. /**
  17. * Convert base64 dataURL to Blob if supported, otherwise returns undefined.
  18. * @private
  19. * @function Highcharts.dataURLtoBlob
  20. * @param {string} dataURL
  21. * URL to convert
  22. * @return {string|undefined}
  23. * Blob
  24. */
  25. var dataURLtoBlob = Highcharts.dataURLtoBlob = function (dataURL) {
  26. var parts = dataURL
  27. .replace(/filename=.*;/, '')
  28. .match(/data:([^;]*)(;base64)?,([0-9A-Za-z+/]+)/);
  29. if (parts &&
  30. parts.length > 3 &&
  31. win.atob &&
  32. win.ArrayBuffer &&
  33. win.Uint8Array &&
  34. win.Blob &&
  35. domurl.createObjectURL) {
  36. // Try to convert data URL to Blob
  37. var binStr = win.atob(parts[3]), buf = new win.ArrayBuffer(binStr.length), binary = new win.Uint8Array(buf);
  38. for (var i = 0; i < binary.length; ++i) {
  39. binary[i] = binStr.charCodeAt(i);
  40. }
  41. var blob = new win.Blob([binary], { 'type': parts[1] });
  42. return domurl.createObjectURL(blob);
  43. }
  44. };
  45. /**
  46. * Download a data URL in the browser. Can also take a blob as first param.
  47. *
  48. * @private
  49. * @function Highcharts.downloadURL
  50. * @param {string|global.URL} dataURL
  51. * The dataURL/Blob to download
  52. * @param {string} filename
  53. * The name of the resulting file (w/extension)
  54. * @return {void}
  55. */
  56. var downloadURL = Highcharts.downloadURL = function (dataURL, filename) {
  57. var nav = win.navigator, a = doc.createElement('a');
  58. // IE specific blob implementation
  59. // Don't use for normal dataURLs
  60. if (typeof dataURL !== 'string' &&
  61. !(dataURL instanceof String) &&
  62. nav.msSaveOrOpenBlob) {
  63. nav.msSaveOrOpenBlob(dataURL, filename);
  64. return;
  65. }
  66. dataURL = "" + dataURL;
  67. // Some browsers have limitations for data URL lengths. Try to convert to
  68. // Blob or fall back. Edge always needs that blob.
  69. var isOldEdgeBrowser = /Edge\/\d+/.test(nav.userAgent);
  70. // Safari on iOS needs Blob in order to download PDF
  71. var safariBlob = (isSafari &&
  72. typeof dataURL === 'string' &&
  73. dataURL.indexOf('data:application/pdf') === 0);
  74. if (safariBlob || isOldEdgeBrowser || dataURL.length > 2000000) {
  75. dataURL = dataURLtoBlob(dataURL) || '';
  76. if (!dataURL) {
  77. throw new Error('Failed to convert to blob');
  78. }
  79. }
  80. // Try HTML5 download attr if supported
  81. if (typeof a.download !== 'undefined') {
  82. a.href = dataURL;
  83. a.download = filename; // HTML5 download attribute
  84. doc.body.appendChild(a);
  85. a.click();
  86. doc.body.removeChild(a);
  87. }
  88. else {
  89. // No download attr, just opening data URI
  90. try {
  91. var windowRef = win.open(dataURL, 'chart');
  92. if (typeof windowRef === 'undefined' || windowRef === null) {
  93. throw new Error('Failed to open window');
  94. }
  95. }
  96. catch (e) {
  97. // window.open failed, trying location.href
  98. win.location.href = dataURL;
  99. }
  100. }
  101. };
  102. var exports = {
  103. dataURLtoBlob: dataURLtoBlob,
  104. downloadURL: downloadURL
  105. };
  106. export default exports;