download_manager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  29. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  30. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  31. ;
  32. var DISABLE_CREATE_OBJECT_URL = _pdf.apiCompatibilityParams.disableCreateObjectURL || false;
  33. function _download(blobUrl, filename) {
  34. var a = document.createElement('a');
  35. if (!a.click) {
  36. throw new Error('DownloadManager: "a.click()" is not supported.');
  37. }
  38. a.href = blobUrl;
  39. a.target = '_parent';
  40. if ('download' in a) {
  41. a.download = filename;
  42. }
  43. (document.body || document.documentElement).appendChild(a);
  44. a.click();
  45. a.remove();
  46. }
  47. var DownloadManager =
  48. /*#__PURE__*/
  49. function () {
  50. function DownloadManager(_ref) {
  51. var _ref$disableCreateObj = _ref.disableCreateObjectURL,
  52. disableCreateObjectURL = _ref$disableCreateObj === void 0 ? DISABLE_CREATE_OBJECT_URL : _ref$disableCreateObj;
  53. _classCallCheck(this, DownloadManager);
  54. this.disableCreateObjectURL = disableCreateObjectURL;
  55. }
  56. _createClass(DownloadManager, [{
  57. key: "downloadUrl",
  58. value: function downloadUrl(url, filename) {
  59. if (!(0, _pdf.createValidAbsoluteUrl)(url, 'http://example.com')) {
  60. return;
  61. }
  62. _download(url + '#pdfjs.action=download', filename);
  63. }
  64. }, {
  65. key: "downloadData",
  66. value: function downloadData(data, filename, contentType) {
  67. if (navigator.msSaveBlob) {
  68. navigator.msSaveBlob(new Blob([data], {
  69. type: contentType
  70. }), filename);
  71. return;
  72. }
  73. var blobUrl = (0, _pdf.createObjectURL)(data, contentType, this.disableCreateObjectURL);
  74. _download(blobUrl, filename);
  75. }
  76. }, {
  77. key: "download",
  78. value: function download(blob, url, filename) {
  79. if (navigator.msSaveBlob) {
  80. if (!navigator.msSaveBlob(blob, filename)) {
  81. this.downloadUrl(url, filename);
  82. }
  83. return;
  84. }
  85. if (this.disableCreateObjectURL) {
  86. this.downloadUrl(url, filename);
  87. return;
  88. }
  89. var blobUrl = URL.createObjectURL(blob);
  90. _download(blobUrl, filename);
  91. }
  92. }]);
  93. return DownloadManager;
  94. }();
  95. exports.DownloadManager = DownloadManager;