Auto-prepend BOM in IE10+

The last commit fixes some BOM prepending issues but it doesn't actually
add support for IE. This commit adds full auto-prepend BOM support for
IE.
This commit is contained in:
Eli Grey
2015-05-07 18:16:34 -04:00
parent f9a09b26f8
commit babc6d9d8f
2 changed files with 20 additions and 15 deletions
+19 -14
View File
@@ -1,6 +1,6 @@
/* FileSaver.js
* A saveAs() FileSaver implementation.
* 2015-05-07.1
* 2015-05-07.2
*
* By Eli Grey, http://eligrey.com
* License: X11/MIT
@@ -12,16 +12,10 @@
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs = saveAs
// IE 10+ (native saveAs)
|| (typeof navigator !== "undefined" &&
navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
// Everyone else
|| (function(view) {
var saveAs = saveAs || (function(view) {
"use strict";
// IE <10 is explicitly unsupported
if (typeof navigator !== "undefined" &&
/MSIE [1-9]\./.test(navigator.userAgent)) {
if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
return;
}
var
@@ -81,7 +75,15 @@ var saveAs = saveAs
}
}
}
, auto_bom = function(blob) {
// prepend BOM for UTF-8 XML and text/* types (including HTML)
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob(["\ufeff", blob], {type: blob.type});
}
return blob;
}
, FileSaver = function(blob, name) {
blob = auto_bom(blob);
// First try a.download, then web filesystem, then object URLs
var
filesaver = this
@@ -125,10 +127,6 @@ var saveAs = saveAs
if (!name) {
name = "download";
}
// prepend BOM for UTF-8 XML and text/* types (including HTML)
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
blob = new Blob(["\ufeff", blob], {type: blob.type});
}
if (can_use_save_link) {
object_url = get_URL().createObjectURL(blob);
save_link.href = object_url;
@@ -211,6 +209,13 @@ var saveAs = saveAs
return new FileSaver(blob, name);
}
;
// IE 10+ (native saveAs)
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
return function(blob, name) {
return navigator.msSaveOrOpenBlob(auto_bom(blob), name);
};
}
FS_proto.abort = function() {
var filesaver = this;
filesaver.readyState = filesaver.DONE;
@@ -245,4 +250,4 @@ if (typeof module !== "undefined" && module.exports) {
define([], function() {
return saveAs;
});
}
}