2
0

download_manager.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. function _download(blobUrl, filename) {
  32. var a = document.createElement('a');
  33. if (!a.click) {
  34. throw new Error('DownloadManager: "a.click()" is not supported.');
  35. }
  36. a.href = blobUrl;
  37. a.target = '_parent';
  38. if ('download' in a) {
  39. a.download = filename;
  40. }
  41. (document.body || document.documentElement).appendChild(a);
  42. a.click();
  43. a.remove();
  44. }
  45. var DownloadManager = function () {
  46. function DownloadManager() {
  47. _classCallCheck(this, DownloadManager);
  48. }
  49. _createClass(DownloadManager, [{
  50. key: 'downloadUrl',
  51. value: function downloadUrl(url, filename) {
  52. if (!(0, _pdf.createValidAbsoluteUrl)(url, 'http://example.com')) {
  53. return;
  54. }
  55. _download(url + '#pdfjs.action=download', filename);
  56. }
  57. }, {
  58. key: 'downloadData',
  59. value: function downloadData(data, filename, contentType) {
  60. if (navigator.msSaveBlob) {
  61. return navigator.msSaveBlob(new Blob([data], { type: contentType }), filename);
  62. }
  63. var blobUrl = (0, _pdf.createObjectURL)(data, contentType, _pdf.PDFJS.disableCreateObjectURL);
  64. _download(blobUrl, filename);
  65. }
  66. }, {
  67. key: 'download',
  68. value: function download(blob, url, filename) {
  69. if (navigator.msSaveBlob) {
  70. if (!navigator.msSaveBlob(blob, filename)) {
  71. this.downloadUrl(url, filename);
  72. }
  73. return;
  74. }
  75. if (_pdf.PDFJS.disableCreateObjectURL) {
  76. this.downloadUrl(url, filename);
  77. return;
  78. }
  79. var blobUrl = URL.createObjectURL(blob);
  80. _download(blobUrl, filename);
  81. }
  82. }]);
  83. return DownloadManager;
  84. }();
  85. exports.DownloadManager = DownloadManager;