| author | ymh <ymh.work@gmail.com> |
| Thu, 31 Mar 2016 17:12:38 +0200 | |
| changeset 598 | eb4f4eceada0 |
| parent 495 | 444b80998255 |
| permissions | -rw-r--r-- |
| 442 | 1 |
/* FileSaver.js |
2 |
* A saveAs() FileSaver implementation. |
|
| 598 | 3 |
* 1.1.20160328 |
| 442 | 4 |
* |
5 |
* By Eli Grey, http://eligrey.com |
|
| 598 | 6 |
* License: MIT |
| 442 | 7 |
* See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md |
8 |
*/ |
|
9 |
||
10 |
/*global self */ |
|
11 |
/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */ |
|
12 |
||
13 |
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */ |
|
14 |
||
| 489 | 15 |
var saveAs = saveAs || (function(view) { |
| 442 | 16 |
"use strict"; |
17 |
// IE <10 is explicitly unsupported |
|
| 489 | 18 |
if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) { |
| 442 | 19 |
return; |
20 |
} |
|
21 |
var |
|
22 |
doc = view.document |
|
23 |
// only get URL when necessary in case Blob.js hasn't overridden it yet |
|
24 |
, get_URL = function() { |
|
25 |
return view.URL || view.webkitURL || view; |
|
26 |
} |
|
27 |
, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a") |
|
28 |
, can_use_save_link = "download" in save_link |
|
29 |
, click = function(node) { |
|
|
495
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
30 |
var event = new MouseEvent("click"); |
| 442 | 31 |
node.dispatchEvent(event); |
32 |
} |
|
| 598 | 33 |
, is_safari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent) |
| 442 | 34 |
, webkit_req_fs = view.webkitRequestFileSystem |
35 |
, req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem |
|
36 |
, throw_outside = function(ex) { |
|
37 |
(view.setImmediate || view.setTimeout)(function() { |
|
38 |
throw ex; |
|
39 |
}, 0); |
|
40 |
} |
|
41 |
, force_saveable_type = "application/octet-stream" |
|
42 |
, fs_min_size = 0 |
|
| 598 | 43 |
// the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to |
44 |
, arbitrary_revoke_timeout = 1000 * 40 // in ms |
|
| 442 | 45 |
, revoke = function(file) { |
46 |
var revoker = function() { |
|
47 |
if (typeof file === "string") { // file is an object URL |
|
48 |
get_URL().revokeObjectURL(file); |
|
49 |
} else { // file is a File |
|
50 |
file.remove(); |
|
51 |
} |
|
52 |
}; |
|
| 598 | 53 |
/* // Take note W3C: |
54 |
var |
|
55 |
uri = typeof file === "string" ? file : file.toURL() |
|
56 |
, revoker = function(evt) { |
|
57 |
// idealy DownloadFinishedEvent.data would be the URL requested |
|
58 |
if (evt.data === uri) { |
|
59 |
if (typeof file === "string") { // file is an object URL |
|
60 |
get_URL().revokeObjectURL(file); |
|
61 |
} else { // file is a File |
|
62 |
file.remove(); |
|
63 |
} |
|
64 |
} |
|
| 442 | 65 |
} |
| 598 | 66 |
; |
67 |
view.addEventListener("downloadfinished", revoker); |
|
68 |
*/ |
|
69 |
setTimeout(revoker, arbitrary_revoke_timeout); |
|
| 442 | 70 |
} |
71 |
, dispatch = function(filesaver, event_types, event) { |
|
72 |
event_types = [].concat(event_types); |
|
73 |
var i = event_types.length; |
|
74 |
while (i--) { |
|
75 |
var listener = filesaver["on" + event_types[i]]; |
|
76 |
if (typeof listener === "function") { |
|
77 |
try { |
|
78 |
listener.call(filesaver, event || filesaver); |
|
79 |
} catch (ex) { |
|
80 |
throw_outside(ex); |
|
81 |
} |
|
82 |
} |
|
83 |
} |
|
84 |
} |
|
| 489 | 85 |
, auto_bom = function(blob) { |
86 |
// prepend BOM for UTF-8 XML and text/* types (including HTML) |
|
87 |
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) { |
|
88 |
return new Blob(["\ufeff", blob], {type: blob.type}); |
|
89 |
} |
|
90 |
return blob; |
|
91 |
} |
|
|
495
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
92 |
, FileSaver = function(blob, name, no_auto_bom) { |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
93 |
if (!no_auto_bom) { |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
94 |
blob = auto_bom(blob); |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
95 |
} |
| 442 | 96 |
// First try a.download, then web filesystem, then object URLs |
97 |
var |
|
98 |
filesaver = this |
|
99 |
, type = blob.type |
|
100 |
, blob_changed = false |
|
101 |
, object_url |
|
102 |
, target_view |
|
103 |
, dispatch_all = function() { |
|
104 |
dispatch(filesaver, "writestart progress write writeend".split(" ")); |
|
105 |
} |
|
106 |
// on any filesys errors revert to saving with object URLs |
|
107 |
, fs_error = function() { |
|
| 598 | 108 |
if (target_view && is_safari && typeof FileReader !== "undefined") { |
109 |
// Safari doesn't allow downloading of blob urls |
|
110 |
var reader = new FileReader(); |
|
111 |
reader.onloadend = function() { |
|
112 |
var base64Data = reader.result; |
|
113 |
target_view.location.href = "data:attachment/file" + base64Data.slice(base64Data.search(/[,;]/)); |
|
114 |
filesaver.readyState = filesaver.DONE; |
|
115 |
dispatch_all(); |
|
116 |
}; |
|
117 |
reader.readAsDataURL(blob); |
|
118 |
filesaver.readyState = filesaver.INIT; |
|
119 |
return; |
|
120 |
} |
|
| 442 | 121 |
// don't create more object URLs than needed |
122 |
if (blob_changed || !object_url) { |
|
123 |
object_url = get_URL().createObjectURL(blob); |
|
124 |
} |
|
125 |
if (target_view) { |
|
126 |
target_view.location.href = object_url; |
|
127 |
} else { |
|
128 |
var new_tab = view.open(object_url, "_blank"); |
|
| 598 | 129 |
if (new_tab === undefined && is_safari) { |
| 442 | 130 |
//Apple do not allow window.open, see http://bit.ly/1kZffRI |
131 |
view.location.href = object_url |
|
132 |
} |
|
133 |
} |
|
134 |
filesaver.readyState = filesaver.DONE; |
|
135 |
dispatch_all(); |
|
136 |
revoke(object_url); |
|
137 |
} |
|
138 |
, abortable = function(func) { |
|
139 |
return function() { |
|
140 |
if (filesaver.readyState !== filesaver.DONE) { |
|
141 |
return func.apply(this, arguments); |
|
142 |
} |
|
143 |
}; |
|
144 |
} |
|
145 |
, create_if_not_found = {create: true, exclusive: false} |
|
146 |
, slice |
|
147 |
; |
|
148 |
filesaver.readyState = filesaver.INIT; |
|
149 |
if (!name) { |
|
150 |
name = "download"; |
|
151 |
} |
|
152 |
if (can_use_save_link) { |
|
153 |
object_url = get_URL().createObjectURL(blob); |
|
|
495
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
154 |
setTimeout(function() { |
| 598 | 155 |
save_link.href = object_url; |
156 |
save_link.download = name; |
|
|
495
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
157 |
click(save_link); |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
158 |
dispatch_all(); |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
159 |
revoke(object_url); |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
160 |
filesaver.readyState = filesaver.DONE; |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
161 |
}); |
| 442 | 162 |
return; |
163 |
} |
|
164 |
// Object and web filesystem URLs have a problem saving in Google Chrome when |
|
165 |
// viewed in a tab, so I force save with application/octet-stream |
|
166 |
// http://code.google.com/p/chromium/issues/detail?id=91158 |
|
167 |
// Update: Google errantly closed 91158, I submitted it again: |
|
168 |
// https://code.google.com/p/chromium/issues/detail?id=389642 |
|
169 |
if (view.chrome && type && type !== force_saveable_type) { |
|
170 |
slice = blob.slice || blob.webkitSlice; |
|
171 |
blob = slice.call(blob, 0, blob.size, force_saveable_type); |
|
172 |
blob_changed = true; |
|
173 |
} |
|
174 |
// Since I can't be sure that the guessed media type will trigger a download |
|
175 |
// in WebKit, I append .download to the filename. |
|
176 |
// https://bugs.webkit.org/show_bug.cgi?id=65440 |
|
177 |
if (webkit_req_fs && name !== "download") { |
|
178 |
name += ".download"; |
|
179 |
} |
|
180 |
if (type === force_saveable_type || webkit_req_fs) { |
|
181 |
target_view = view; |
|
182 |
} |
|
183 |
if (!req_fs) { |
|
184 |
fs_error(); |
|
185 |
return; |
|
186 |
} |
|
187 |
fs_min_size += blob.size; |
|
188 |
req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) { |
|
189 |
fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) { |
|
190 |
var save = function() { |
|
191 |
dir.getFile(name, create_if_not_found, abortable(function(file) { |
|
192 |
file.createWriter(abortable(function(writer) { |
|
193 |
writer.onwriteend = function(event) { |
|
194 |
target_view.location.href = file.toURL(); |
|
195 |
filesaver.readyState = filesaver.DONE; |
|
196 |
dispatch(filesaver, "writeend", event); |
|
197 |
revoke(file); |
|
198 |
}; |
|
199 |
writer.onerror = function() { |
|
200 |
var error = writer.error; |
|
201 |
if (error.code !== error.ABORT_ERR) { |
|
202 |
fs_error(); |
|
203 |
} |
|
204 |
}; |
|
205 |
"writestart progress write abort".split(" ").forEach(function(event) { |
|
206 |
writer["on" + event] = filesaver["on" + event]; |
|
207 |
}); |
|
208 |
writer.write(blob); |
|
209 |
filesaver.abort = function() { |
|
210 |
writer.abort(); |
|
211 |
filesaver.readyState = filesaver.DONE; |
|
212 |
}; |
|
213 |
filesaver.readyState = filesaver.WRITING; |
|
214 |
}), fs_error); |
|
215 |
}), fs_error); |
|
216 |
}; |
|
217 |
dir.getFile(name, {create: false}, abortable(function(file) { |
|
218 |
// delete file if it already exists |
|
219 |
file.remove(); |
|
220 |
save(); |
|
221 |
}), abortable(function(ex) { |
|
222 |
if (ex.code === ex.NOT_FOUND_ERR) { |
|
223 |
save(); |
|
224 |
} else { |
|
225 |
fs_error(); |
|
226 |
} |
|
227 |
})); |
|
228 |
}), fs_error); |
|
229 |
}), fs_error); |
|
230 |
} |
|
231 |
, FS_proto = FileSaver.prototype |
|
|
495
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
232 |
, saveAs = function(blob, name, no_auto_bom) { |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
233 |
return new FileSaver(blob, name, no_auto_bom); |
| 442 | 234 |
} |
235 |
; |
|
| 489 | 236 |
// IE 10+ (native saveAs) |
237 |
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) { |
|
|
495
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
238 |
return function(blob, name, no_auto_bom) { |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
239 |
if (!no_auto_bom) { |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
240 |
blob = auto_bom(blob); |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
241 |
} |
|
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
242 |
return navigator.msSaveOrOpenBlob(blob, name || "download"); |
| 489 | 243 |
}; |
244 |
} |
|
245 |
||
| 442 | 246 |
FS_proto.abort = function() { |
247 |
var filesaver = this; |
|
248 |
filesaver.readyState = filesaver.DONE; |
|
249 |
dispatch(filesaver, "abort"); |
|
250 |
}; |
|
251 |
FS_proto.readyState = FS_proto.INIT = 0; |
|
252 |
FS_proto.WRITING = 1; |
|
253 |
FS_proto.DONE = 2; |
|
254 |
||
255 |
FS_proto.error = |
|
256 |
FS_proto.onwritestart = |
|
257 |
FS_proto.onprogress = |
|
258 |
FS_proto.onwrite = |
|
259 |
FS_proto.onabort = |
|
260 |
FS_proto.onerror = |
|
261 |
FS_proto.onwriteend = |
|
262 |
null; |
|
263 |
||
264 |
return saveAs; |
|
265 |
}( |
|
266 |
typeof self !== "undefined" && self |
|
267 |
|| typeof window !== "undefined" && window |
|
268 |
|| this.content |
|
269 |
)); |
|
270 |
// `self` is undefined in Firefox for Android content script context |
|
271 |
// while `this` is nsIContentFrameMessageManager |
|
272 |
// with an attribute `content` that corresponds to the window |
|
273 |
||
274 |
if (typeof module !== "undefined" && module.exports) { |
|
| 489 | 275 |
module.exports.saveAs = saveAs; |
| 598 | 276 |
} else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) { |
| 442 | 277 |
define([], function() { |
278 |
return saveAs; |
|
279 |
}); |
|
|
495
444b80998255
update client version + small correction in dojoConfig to load ckeditor jquery adapter
ymh <ymh.work@gmail.com>
parents:
489
diff
changeset
|
280 |
} |