2 * A Blob implementation.
5 * By Eli Grey, http://eligrey.com
6 * By Devin Samarin, https://github.com/eboyjr
11 /*global self, unescape */
12 /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
15 /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
17 if (!(typeof Blob === "function" || typeof Blob === "object") || typeof URL === "undefined")
18 if ((typeof Blob === "function" || typeof Blob === "object") && typeof webkitURL !== "undefined") self.URL = webkitURL;
19 else var Blob = (function (view) {
22 var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || view.MSBlobBuilder || (function(view) {
24 get_class = function(object) {
25 return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
27 , FakeBlobBuilder = function BlobBuilder() {
30 , FakeBlob = function Blob(data, type, encoding) {
32 this.size = data.length;
34 this.encoding = encoding;
36 , FBB_proto = FakeBlobBuilder.prototype
37 , FB_proto = FakeBlob.prototype
38 , FileReaderSync = view.FileReaderSync
39 , FileException = function(type) {
40 this.code = this[this.name = type];
43 "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
44 + "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
46 , file_ex_code = file_ex_codes.length
47 , real_URL = view.URL || view.webkitURL || view
48 , real_create_object_URL = real_URL.createObjectURL
49 , real_revoke_object_URL = real_URL.revokeObjectURL
54 , ArrayBuffer = view.ArrayBuffer
55 , Uint8Array = view.Uint8Array
57 FakeBlob.fake = FB_proto.fake = true;
58 while (file_ex_code--) {
59 FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
61 if (!real_URL.createObjectURL) {
64 URL.createObjectURL = function(blob) {
70 type = "application/octet-stream";
72 if (blob instanceof FakeBlob) {
73 data_URI_header = "data:" + type;
74 if (blob.encoding === "base64") {
75 return data_URI_header + ";base64," + blob.data;
76 } else if (blob.encoding === "URI") {
77 return data_URI_header + "," + decodeURIComponent(blob.data);
79 return data_URI_header + ";base64," + btoa(blob.data);
81 return data_URI_header + "," + encodeURIComponent(blob.data);
83 } else if (real_create_object_URL) {
84 return real_create_object_URL.call(real_URL, blob);
87 URL.revokeObjectURL = function(object_URL) {
88 if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
89 real_revoke_object_URL.call(real_URL, object_URL);
92 FBB_proto.append = function(data/*, endings*/) {
94 // decode data to a binary string
95 if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
98 , buf = new Uint8Array(data)
100 , buf_len = buf.length
102 for (; i < buf_len; i++) {
103 str += String.fromCharCode(buf[i]);
106 } else if (get_class(data) === "Blob" || get_class(data) === "File") {
107 if (FileReaderSync) {
108 var fr = new FileReaderSync;
109 bb.push(fr.readAsBinaryString(data));
111 // async FileReader won't work as BlobBuilder is sync
112 throw new FileException("NOT_READABLE_ERR");
114 } else if (data instanceof FakeBlob) {
115 if (data.encoding === "base64" && atob) {
116 bb.push(atob(data.data));
117 } else if (data.encoding === "URI") {
118 bb.push(decodeURIComponent(data.data));
119 } else if (data.encoding === "raw") {
123 if (typeof data !== "string") {
124 data += ""; // convert unsupported types to strings
126 // decode UTF-16 to binary string
127 bb.push(unescape(encodeURIComponent(data)));
130 FBB_proto.getBlob = function(type) {
131 if (!arguments.length) {
134 return new FakeBlob(this.data.join(""), type, "raw");
136 FBB_proto.toString = function() {
137 return "[object BlobBuilder]";
139 FB_proto.slice = function(start, end, type) {
140 var args = arguments.length;
145 this.data.slice(start, args > 1 ? end : this.data.length)
150 FB_proto.toString = function() {
151 return "[object Blob]";
153 return FakeBlobBuilder;
156 return function Blob(blobParts, options) {
157 var type = options ? (options.type || "") : "";
158 var builder = new BlobBuilder();
160 for (var i = 0, len = blobParts.length; i < len; i++) {
161 builder.append(blobParts[i]);
164 return builder.getBlob(type);
169 * A saveAs() FileSaver implementation.
172 * By Eli Grey, http://eligrey.com
178 /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
181 /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
184 || (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
189 // only get URL when necessary in case BlobBuilder.js hasn't overridden it yet
190 , get_URL = function() {
191 return view.URL || view.webkitURL || view;
193 , URL = view.URL || view.webkitURL || view
194 , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
195 , can_use_save_link = !view.externalHost && "download" in save_link
196 , click = function(node) {
197 var event = doc.createEvent("MouseEvents");
198 event.initMouseEvent(
199 "click", true, false, view, 0, 0, 0, 0, 0
200 , false, false, false, false, 0, null
202 node.dispatchEvent(event);
204 , webkit_req_fs = view.webkitRequestFileSystem
205 , req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
206 , throw_outside = function (ex) {
207 (view.setImmediate || view.setTimeout)(function() {
211 , force_saveable_type = "application/octet-stream"
213 , deletion_queue = []
214 , process_deletion_queue = function() {
215 var i = deletion_queue.length;
217 var file = deletion_queue[i];
218 if (typeof file === "string") { // file is an object URL
219 URL.revokeObjectURL(file);
220 } else { // file is a File
224 deletion_queue.length = 0; // clear queue
226 , dispatch = function(filesaver, event_types, event) {
227 event_types = [].concat(event_types);
228 var i = event_types.length;
230 var listener = filesaver["on" + event_types[i]];
231 if (typeof listener === "function") {
233 listener.call(filesaver, event || filesaver);
240 , FileSaver = function(blob, name) {
241 // First try a.download, then web filesystem, then object URLs
245 , blob_changed = false
248 , get_object_url = function() {
249 var object_url = get_URL().createObjectURL(blob);
250 deletion_queue.push(object_url);
253 , dispatch_all = function() {
254 dispatch(filesaver, "writestart progress write writeend".split(" "));
256 // on any filesys errors revert to saving with object URLs
257 , fs_error = function() {
258 // don't create more object URLs than needed
259 if (blob_changed || !object_url) {
260 object_url = get_object_url(blob);
263 target_view.location.href = object_url;
265 window.open(object_url, "_blank");
267 filesaver.readyState = filesaver.DONE;
270 , abortable = function(func) {
272 if (filesaver.readyState !== filesaver.DONE) {
273 return func.apply(this, arguments);
277 , create_if_not_found = {create: true, exclusive: false}
280 filesaver.readyState = filesaver.INIT;
284 if (can_use_save_link) {
285 object_url = get_object_url(blob);
286 // FF for Android has a nasty garbage collection mechanism
287 // that turns all objects that are not pure javascript into 'deadObject'
288 // this means `doc` and `save_link` are unusable and need to be recreated
289 // `view` is usable though:
291 save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a");
292 save_link.href = object_url;
293 save_link.download = name;
294 var event = doc.createEvent("MouseEvents");
295 event.initMouseEvent(
296 "click", true, false, view, 0, 0, 0, 0, 0
297 , false, false, false, false, 0, null
299 save_link.dispatchEvent(event);
300 filesaver.readyState = filesaver.DONE;
304 // Object and web filesystem URLs have a problem saving in Google Chrome when
305 // viewed in a tab, so I force save with application/octet-stream
306 // http://code.google.com/p/chromium/issues/detail?id=91158
307 if (view.chrome && type && type !== force_saveable_type) {
308 slice = blob.slice || blob.webkitSlice;
309 blob = slice.call(blob, 0, blob.size, force_saveable_type);
312 // Since I can't be sure that the guessed media type will trigger a download
313 // in WebKit, I append .download to the filename.
314 // https://bugs.webkit.org/show_bug.cgi?id=65440
315 if (webkit_req_fs && name !== "download") {
318 if (type === force_saveable_type || webkit_req_fs) {
325 fs_min_size += blob.size;
326 req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) {
327 fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) {
328 var save = function() {
329 dir.getFile(name, create_if_not_found, abortable(function(file) {
330 file.createWriter(abortable(function(writer) {
331 writer.onwriteend = function(event) {
332 target_view.location.href = file.toURL();
333 deletion_queue.push(file);
334 filesaver.readyState = filesaver.DONE;
335 dispatch(filesaver, "writeend", event);
337 writer.onerror = function() {
338 var error = writer.error;
339 if (error.code !== error.ABORT_ERR) {
343 "writestart progress write abort".split(" ").forEach(function(event) {
344 writer["on" + event] = filesaver["on" + event];
347 filesaver.abort = function() {
349 filesaver.readyState = filesaver.DONE;
351 filesaver.readyState = filesaver.WRITING;
355 dir.getFile(name, {create: false}, abortable(function(file) {
356 // delete file if it already exists
359 }), abortable(function(ex) {
360 if (ex.code === ex.NOT_FOUND_ERR) {
369 , FS_proto = FileSaver.prototype
370 , saveAs = function(blob, name) {
371 return new FileSaver(blob, name);
374 FS_proto.abort = function() {
375 var filesaver = this;
376 filesaver.readyState = filesaver.DONE;
377 dispatch(filesaver, "abort");
379 FS_proto.readyState = FS_proto.INIT = 0;
380 FS_proto.WRITING = 1;
384 FS_proto.onwritestart =
385 FS_proto.onprogress =
389 FS_proto.onwriteend =
392 view.addEventListener("unload", process_deletion_queue, false);
394 }(this.self || this.window || this.content));
395 // `self` is undefined in Firefox for Android content script context
396 // while `this` is nsIContentFrameMessageManager
397 // with an attribute `content` that corresponds to the window
399 if (typeof module !== 'undefined') module.exports = saveAs;