2
0

download_manager.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.DownloadManager = undefined;
  20. 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; }; }();
  21. var _pdf = require('../pdf');
  22. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  23. ;
  24. function _download(blobUrl, filename) {
  25. var a = document.createElement('a');
  26. if (a.click) {
  27. a.href = blobUrl;
  28. a.target = '_parent';
  29. if ('download' in a) {
  30. a.download = filename;
  31. }
  32. (document.body || document.documentElement).appendChild(a);
  33. a.click();
  34. a.parentNode.removeChild(a);
  35. } else {
  36. if (window.top === window && blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
  37. var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';
  38. blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
  39. }
  40. window.open(blobUrl, '_parent');
  41. }
  42. }
  43. var DownloadManager = function () {
  44. function DownloadManager() {
  45. _classCallCheck(this, DownloadManager);
  46. }
  47. _createClass(DownloadManager, [{
  48. key: 'downloadUrl',
  49. value: function downloadUrl(url, filename) {
  50. if (!(0, _pdf.createValidAbsoluteUrl)(url, 'http://example.com')) {
  51. return;
  52. }
  53. _download(url + '#pdfjs.action=download', filename);
  54. }
  55. }, {
  56. key: 'downloadData',
  57. value: function downloadData(data, filename, contentType) {
  58. if (navigator.msSaveBlob) {
  59. return navigator.msSaveBlob(new Blob([data], { type: contentType }), filename);
  60. }
  61. var blobUrl = (0, _pdf.createObjectURL)(data, contentType, _pdf.PDFJS.disableCreateObjectURL);
  62. _download(blobUrl, filename);
  63. }
  64. }, {
  65. key: 'download',
  66. value: function download(blob, url, filename) {
  67. if (navigator.msSaveBlob) {
  68. if (!navigator.msSaveBlob(blob, filename)) {
  69. this.downloadUrl(url, filename);
  70. }
  71. return;
  72. }
  73. if (_pdf.PDFJS.disableCreateObjectURL) {
  74. this.downloadUrl(url, filename);
  75. return;
  76. }
  77. var blobUrl = URL.createObjectURL(blob);
  78. _download(blobUrl, filename);
  79. }
  80. }]);
  81. return DownloadManager;
  82. }();
  83. exports.DownloadManager = DownloadManager;