Improve download button and behaviour
Fix download button for top frames and sub frames When PDF.js is the top frame, and the PDF URL is identical to the top URL, download would fail. Fixed by adding a ? or & in these cases. When PDF.js is embedded in a frame from a different origin, download would fail because window.open(url, '_parent') is ignored. Fixed by using a.click() when available. a.click() works in Chrome 25, Firefox 19, Opera 12.00 and IE 8. Safari 5.1 does not support a.click() Use a.download if available + documentation
This commit is contained in:
parent
b46c375126
commit
88767e1861
@ -1063,8 +1063,43 @@ var PDFView = {
|
||||
}
|
||||
var url = this.url.split('#')[0];
|
||||
//#if !(FIREFOX || MOZCENTRAL)
|
||||
|
||||
var a = document.createElement('a');
|
||||
|
||||
// If _parent == self, then opening an identical URL with different
|
||||
// location hash will only cause a navigation, not a download.
|
||||
if (window.top === window && !('download' in a) &&
|
||||
url === window.location.href.split('#')[0]) {
|
||||
url += url.indexOf('?') === -1 ? '?' : '&';
|
||||
}
|
||||
|
||||
url += '#pdfjs.action=download';
|
||||
window.open(url, '_parent');
|
||||
if (a.click) {
|
||||
// Use a.click() if available. Otherwise, Chrome might show
|
||||
// "Unsafe JavaScript attempt to initiate a navigation change
|
||||
// for frame with URL" and not open the PDF at all.
|
||||
// Supported by (not mentioned = untested):
|
||||
// - Firefox 6 - 19 (4- does not support a.click, 5 ignores a.click)
|
||||
// - Chrome 19 - 26 (18- does not support a.click)
|
||||
// - Opera 9 - 12.15
|
||||
// - Internet Explorer 6 - 10
|
||||
// - Safari 6 (5.1- does not support a.click)
|
||||
a.href = url;
|
||||
a.target = '_parent';
|
||||
// Use a.download if available. This increases the likelihood that
|
||||
// the file is downloaded instead of opened by another PDF plugin.
|
||||
if ('download' in a) {
|
||||
var filename = url.match(/([^\/?#=]+\.pdf)/i);
|
||||
a.download = filename ? filename[1] : 'file.pdf';
|
||||
}
|
||||
// <a> must be in the document for IE and recent Firefox versions.
|
||||
// (otherwise .click() is ignored)
|
||||
(document.body || document.documentElement).appendChild(a);
|
||||
a.click();
|
||||
a.parentNode.removeChild(a);
|
||||
} else {
|
||||
window.open(url, '_parent');
|
||||
}
|
||||
//#else
|
||||
// // Document isn't ready just try to download with the url.
|
||||
// if (!this.pdfDocument) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user