stringTools.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. export class StringTools {
  2. /*
  3. * Based on FileSaver.js
  4. * A saveAs() FileSaver implementation.
  5. *
  6. * By Eli Grey, http://eligrey.com
  7. *
  8. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  9. * source : http://purl.eligrey.com/github/FileSaver.js
  10. */
  11. private static _SaveAs(blob: Blob, name: string, document: HTMLDocument) {
  12. if ('download' in HTMLAnchorElement.prototype) {
  13. var URL = window.URL || window.webkitURL;
  14. var a = document.createElement('a');
  15. a.download = name;
  16. a.rel = 'noopener'; // tabnabbing
  17. a.href = URL.createObjectURL(blob)
  18. setTimeout(() => { URL.revokeObjectURL(a.href) }, 4E4) // 40s
  19. setTimeout(() => { this._Click(a, document) }, 0);
  20. return;
  21. }
  22. // Open a popup immediately do go around popup blocker
  23. // Mostly only available on user interaction and the fileReader is async so...
  24. var popup = open('', '_blank');
  25. if (popup) {
  26. popup.document.title = popup.document.body.innerText = 'downloading...';
  27. }
  28. var force = blob.type === 'application/octet-stream';
  29. var isSafari = /constructor/i.test((window as any).HTMLElement) || (window as any).safari;
  30. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  31. if ((isChromeIOS || (force && isSafari)) && typeof FileReader !== 'undefined') {
  32. // Safari doesn't allow downloading of blob URLs
  33. var reader = new FileReader();
  34. reader.onloadend = () => {
  35. var url:any = reader.result;
  36. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  37. if (popup) {
  38. popup.location.href = url;
  39. }
  40. else {
  41. location = url;
  42. }
  43. popup = null;
  44. }
  45. reader.readAsDataURL(blob);
  46. } else {
  47. var URL = window.URL || window.webkitURL
  48. var url = URL.createObjectURL(blob)
  49. if (popup) {
  50. popup.location.href = url;
  51. }
  52. else {
  53. location.href = url;
  54. }
  55. popup = null;
  56. setTimeout(function () { URL.revokeObjectURL(url) }, 4E4);
  57. }
  58. }
  59. private static _Click(node: HTMLElement, document: HTMLDocument) {
  60. try {
  61. node.dispatchEvent(new MouseEvent('click'))
  62. } catch (e) {
  63. var evt = document.createEvent('MouseEvents');
  64. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  65. node.dispatchEvent(evt);
  66. }
  67. }
  68. /**
  69. * Download a string into a file that will be saved locally by the browser
  70. * @param content defines the string to download locally as a file
  71. */
  72. public static DownloadAsFile(document: HTMLDocument, content: string, filename: string) {
  73. let blob = new Blob([content],
  74. {
  75. type: "application/octet-stream"
  76. });
  77. this._SaveAs(blob, filename, document);
  78. }
  79. }