download_manager.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DownloadManager = void 0;
  27. var _pdf = require("../pdf");
  28. var _viewer_compatibility = require("./viewer_compatibility.js");
  29. ;
  30. function download(blobUrl, filename) {
  31. const a = document.createElement("a");
  32. if (!a.click) {
  33. throw new Error('DownloadManager: "a.click()" is not supported.');
  34. }
  35. a.href = blobUrl;
  36. a.target = "_parent";
  37. if ("download" in a) {
  38. a.download = filename;
  39. }
  40. (document.body || document.documentElement).appendChild(a);
  41. a.click();
  42. a.remove();
  43. }
  44. class DownloadManager {
  45. downloadUrl(url, filename) {
  46. if (!(0, _pdf.createValidAbsoluteUrl)(url, "http://example.com")) {
  47. return;
  48. }
  49. download(url + "#pdfjs.action=download", filename);
  50. }
  51. downloadData(data, filename, contentType) {
  52. if (navigator.msSaveBlob) {
  53. navigator.msSaveBlob(new Blob([data], {
  54. type: contentType
  55. }), filename);
  56. return;
  57. }
  58. const blobUrl = (0, _pdf.createObjectURL)(data, contentType, _viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL);
  59. download(blobUrl, filename);
  60. }
  61. download(blob, url, filename, sourceEventType = "download") {
  62. if (navigator.msSaveBlob) {
  63. if (!navigator.msSaveBlob(blob, filename)) {
  64. this.downloadUrl(url, filename);
  65. }
  66. return;
  67. }
  68. if (_viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL) {
  69. this.downloadUrl(url, filename);
  70. return;
  71. }
  72. const blobUrl = URL.createObjectURL(blob);
  73. download(blobUrl, filename);
  74. }
  75. }
  76. exports.DownloadManager = DownloadManager;