download_manager.js 3.6 KB

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