Skip to content

Commit babc6d9

Browse files
committed
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.
1 parent f9a09b2 commit babc6d9

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

FileSaver.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* FileSaver.js
22
* A saveAs() FileSaver implementation.
3-
* 2015-05-07.1
3+
* 2015-05-07.2
44
*
55
* By Eli Grey, http://eligrey.com
66
* License: X11/MIT
@@ -12,16 +12,10 @@
1212

1313
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
1414

15-
var saveAs = saveAs
16-
// IE 10+ (native saveAs)
17-
|| (typeof navigator !== "undefined" &&
18-
navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
19-
// Everyone else
20-
|| (function(view) {
15+
var saveAs = saveAs || (function(view) {
2116
"use strict";
2217
// IE <10 is explicitly unsupported
23-
if (typeof navigator !== "undefined" &&
24-
/MSIE [1-9]\./.test(navigator.userAgent)) {
18+
if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
2519
return;
2620
}
2721
var
@@ -81,7 +75,15 @@ var saveAs = saveAs
8175
}
8276
}
8377
}
78+
, auto_bom = function(blob) {
79+
// prepend BOM for UTF-8 XML and text/* types (including HTML)
80+
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
81+
return new Blob(["\ufeff", blob], {type: blob.type});
82+
}
83+
return blob;
84+
}
8485
, FileSaver = function(blob, name) {
86+
blob = auto_bom(blob);
8587
// First try a.download, then web filesystem, then object URLs
8688
var
8789
filesaver = this
@@ -125,10 +127,6 @@ var saveAs = saveAs
125127
if (!name) {
126128
name = "download";
127129
}
128-
// prepend BOM for UTF-8 XML and text/* types (including HTML)
129-
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
130-
blob = new Blob(["\ufeff", blob], {type: blob.type});
131-
}
132130
if (can_use_save_link) {
133131
object_url = get_URL().createObjectURL(blob);
134132
save_link.href = object_url;
@@ -211,6 +209,13 @@ var saveAs = saveAs
211209
return new FileSaver(blob, name);
212210
}
213211
;
212+
// IE 10+ (native saveAs)
213+
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
214+
return function(blob, name) {
215+
return navigator.msSaveOrOpenBlob(auto_bom(blob), name);
216+
};
217+
}
218+
214219
FS_proto.abort = function() {
215220
var filesaver = this;
216221
filesaver.readyState = filesaver.DONE;
@@ -245,4 +250,4 @@ if (typeof module !== "undefined" && module.exports) {
245250
define([], function() {
246251
return saveAs;
247252
});
248-
}
253+
}

FileSaver.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)