Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to FileReader in iOS Chrome #612

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
23 changes: 11 additions & 12 deletions dist/FileSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
return blob;
}

function download(url, name, opts) {
function download(url, name, opts, popup) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'blob';

xhr.onload = function () {
saveAs(xhr.response, name, opts);
saveAs(xhr.response, name, opts, popup);
};

xhr.onerror = function () {
Expand Down Expand Up @@ -83,22 +83,23 @@
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
node.dispatchEvent(evt);
}
} // Detect WebKit inside a native macOS app

}

var isWebKit = /AppleWebKit/.test(navigator.userAgent);
var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
var saveAs = _global.saveAs || ( // probably in some web worker
typeof window !== 'object' || window !== _global ? function saveAs() {}
/* noop */
// Use download attribute first if possible (#193 Lumia mobile) unless this is a native macOS app
: 'download' in HTMLAnchorElement.prototype && !isWebKit ? function saveAs(blob, name, opts) {
// Use download attribute first if possible (#193 Lumia mobile)
// Except of Chrome iOS because it behaves incorrectly on iOS 13
: 'download' in HTMLAnchorElement.prototype && !isChromeIOS ? function saveAs(blob, name, opts) {
var URL = _global.URL || _global.webkitURL;
var a = document.createElement('a');
name = name || blob.name || 'download';
a.download = name;
a.rel = 'noopener'; // tabnabbing
// TODO: detect chrome extensions & packaged apps
// a.target = '_blank'

a.target = '_blank';

if (typeof blob === 'string') {
// Support regular links
Expand Down Expand Up @@ -148,14 +149,12 @@
popup.document.title = popup.document.body.innerText = 'downloading...';
}

if (typeof blob === 'string') return download(blob, name, opts);
if (typeof blob === 'string') return download(blob, name, opts, popup);
var force = blob.type === 'application/octet-stream';

var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;

var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);

if ((isChromeIOS || force && isSafari || isWebKit) && typeof FileReader !== 'undefined') {
if ((isChromeIOS || force && isSafari) && typeof FileReader !== 'undefined') {
// Safari doesn't allow downloading of blob URLs
var reader = new FileReader();

Expand Down
2 changes: 1 addition & 1 deletion dist/FileSaver.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading