download_manager.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 _pdf = require('../pdf');
  21. ;
  22. function download(blobUrl, filename) {
  23. var a = document.createElement('a');
  24. if (a.click) {
  25. a.href = blobUrl;
  26. a.target = '_parent';
  27. if ('download' in a) {
  28. a.download = filename;
  29. }
  30. (document.body || document.documentElement).appendChild(a);
  31. a.click();
  32. a.parentNode.removeChild(a);
  33. } else {
  34. if (window.top === window && blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
  35. var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';
  36. blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
  37. }
  38. window.open(blobUrl, '_parent');
  39. }
  40. }
  41. function DownloadManager() {}
  42. DownloadManager.prototype = {
  43. downloadUrl: function DownloadManager_downloadUrl(url, filename) {
  44. if (!(0, _pdf.createValidAbsoluteUrl)(url, 'http://example.com')) {
  45. return;
  46. }
  47. download(url + '#pdfjs.action=download', filename);
  48. },
  49. downloadData: function DownloadManager_downloadData(data, filename, contentType) {
  50. if (navigator.msSaveBlob) {
  51. return navigator.msSaveBlob(new Blob([data], { type: contentType }), filename);
  52. }
  53. var blobUrl = (0, _pdf.createObjectURL)(data, contentType, _pdf.PDFJS.disableCreateObjectURL);
  54. download(blobUrl, filename);
  55. },
  56. download: function DownloadManager_download(blob, url, filename) {
  57. if (navigator.msSaveBlob) {
  58. if (!navigator.msSaveBlob(blob, filename)) {
  59. this.downloadUrl(url, filename);
  60. }
  61. return;
  62. }
  63. if (_pdf.PDFJS.disableCreateObjectURL) {
  64. this.downloadUrl(url, filename);
  65. return;
  66. }
  67. var blobUrl = URL.createObjectURL(blob);
  68. download(blobUrl, filename);
  69. }
  70. };
  71. exports.DownloadManager = DownloadManager;