|
| 1 | +/* FileSaver.js |
| 2 | + * A saveAs() FileSaver implementation. |
| 3 | + * 1.2.2 |
| 4 | + * |
| 5 | + * By Eli Grey, http://eligrey.com |
| 6 | + * License: MIT |
| 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 | + |
| 15 | +var saveAs = saveAs || (function(view) { |
| 16 | + "use strict"; |
| 17 | + // IE <10 is explicitly unsupported |
| 18 | + if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) { |
| 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) { |
| 30 | + var event = new MouseEvent("click"); |
| 31 | + node.dispatchEvent(event); |
| 32 | + } |
| 33 | + , is_safari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent) |
| 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 |
| 43 | + // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to |
| 44 | + , arbitrary_revoke_timeout = 1000 * 40 // in ms |
| 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 | + }; |
| 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 | + } |
| 65 | + } |
| 66 | + ; |
| 67 | + view.addEventListener("downloadfinished", revoker); |
| 68 | + */ |
| 69 | + setTimeout(revoker, arbitrary_revoke_timeout); |
| 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 | + } |
| 85 | + , auto_bom = function(blob) { |
| 86 | + // prepend BOM for UTF-8 XML and text/* types (including HTML) |
| 87 | + // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF |
| 88 | + if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) { |
| 89 | + return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type}); |
| 90 | + } |
| 91 | + return blob; |
| 92 | + } |
| 93 | + , FileSaver = function(blob, name, no_auto_bom) { |
| 94 | + if (!no_auto_bom) { |
| 95 | + blob = auto_bom(blob); |
| 96 | + } |
| 97 | + // First try a.download, then web filesystem, then object URLs |
| 98 | + var |
| 99 | + filesaver = this |
| 100 | + , type = blob.type |
| 101 | + , blob_changed = false |
| 102 | + , object_url |
| 103 | + , target_view |
| 104 | + , dispatch_all = function() { |
| 105 | + dispatch(filesaver, "writestart progress write writeend".split(" ")); |
| 106 | + } |
| 107 | + // on any filesys errors revert to saving with object URLs |
| 108 | + , fs_error = function() { |
| 109 | + if (target_view && is_safari && typeof FileReader !== "undefined") { |
| 110 | + // Safari doesn't allow downloading of blob urls |
| 111 | + var reader = new FileReader(); |
| 112 | + reader.onloadend = function() { |
| 113 | + var base64Data = reader.result; |
| 114 | + target_view.location.href = "data:attachment/file" + base64Data.slice(base64Data.search(/[,;]/)); |
| 115 | + filesaver.readyState = filesaver.DONE; |
| 116 | + dispatch_all(); |
| 117 | + }; |
| 118 | + reader.readAsDataURL(blob); |
| 119 | + filesaver.readyState = filesaver.INIT; |
| 120 | + return; |
| 121 | + } |
| 122 | + // don't create more object URLs than needed |
| 123 | + if (blob_changed || !object_url) { |
| 124 | + object_url = get_URL().createObjectURL(blob); |
| 125 | + } |
| 126 | + if (target_view) { |
| 127 | + target_view.location.href = object_url; |
| 128 | + } else { |
| 129 | + var new_tab = view.open(object_url, "_blank"); |
| 130 | + if (new_tab === undefined && is_safari) { |
| 131 | + // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html |
| 132 | + view.location.href = object_url; |
| 133 | + } |
| 134 | + } |
| 135 | + filesaver.readyState = filesaver.DONE; |
| 136 | + dispatch_all(); |
| 137 | + revoke(object_url); |
| 138 | + } |
| 139 | + , abortable = function(func) { |
| 140 | + return function() { |
| 141 | + if (filesaver.readyState !== filesaver.DONE) { |
| 142 | + return func.apply(this, arguments); |
| 143 | + } |
| 144 | + }; |
| 145 | + } |
| 146 | + , create_if_not_found = {create: true, exclusive: false} |
| 147 | + , slice |
| 148 | + ; |
| 149 | + filesaver.readyState = filesaver.INIT; |
| 150 | + if (!name) { |
| 151 | + name = "download"; |
| 152 | + } |
| 153 | + if (can_use_save_link) { |
| 154 | + object_url = get_URL().createObjectURL(blob); |
| 155 | + setTimeout(function() { |
| 156 | + save_link.href = object_url; |
| 157 | + save_link.download = name; |
| 158 | + click(save_link); |
| 159 | + dispatch_all(); |
| 160 | + revoke(object_url); |
| 161 | + filesaver.readyState = filesaver.DONE; |
| 162 | + }); |
| 163 | + return; |
| 164 | + } |
| 165 | + // Object and web filesystem URLs have a problem saving in Google Chrome when |
| 166 | + // viewed in a tab, so I force save with application/octet-stream |
| 167 | + // http://code.google.com/p/chromium/issues/detail?id=91158 |
| 168 | + // Update: Google errantly closed 91158, I submitted it again: |
| 169 | + // https://code.google.com/p/chromium/issues/detail?id=389642 |
| 170 | + if (view.chrome && type && type !== force_saveable_type) { |
| 171 | + slice = blob.slice || blob.webkitSlice; |
| 172 | + blob = slice.call(blob, 0, blob.size, force_saveable_type); |
| 173 | + blob_changed = true; |
| 174 | + } |
| 175 | + // Since I can't be sure that the guessed media type will trigger a download |
| 176 | + // in WebKit, I append .download to the filename. |
| 177 | + // https://bugs.webkit.org/show_bug.cgi?id=65440 |
| 178 | + if (webkit_req_fs && name !== "download") { |
| 179 | + name += ".download"; |
| 180 | + } |
| 181 | + if (type === force_saveable_type || webkit_req_fs) { |
| 182 | + target_view = view; |
| 183 | + } |
| 184 | + if (!req_fs) { |
| 185 | + fs_error(); |
| 186 | + return; |
| 187 | + } |
| 188 | + fs_min_size += blob.size; |
| 189 | + req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) { |
| 190 | + fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) { |
| 191 | + var save = function() { |
| 192 | + dir.getFile(name, create_if_not_found, abortable(function(file) { |
| 193 | + file.createWriter(abortable(function(writer) { |
| 194 | + writer.onwriteend = function(event) { |
| 195 | + target_view.location.href = file.toURL(); |
| 196 | + filesaver.readyState = filesaver.DONE; |
| 197 | + dispatch(filesaver, "writeend", event); |
| 198 | + revoke(file); |
| 199 | + }; |
| 200 | + writer.onerror = function() { |
| 201 | + var error = writer.error; |
| 202 | + if (error.code !== error.ABORT_ERR) { |
| 203 | + fs_error(); |
| 204 | + } |
| 205 | + }; |
| 206 | + "writestart progress write abort".split(" ").forEach(function(event) { |
| 207 | + writer["on" + event] = filesaver["on" + event]; |
| 208 | + }); |
| 209 | + writer.write(blob); |
| 210 | + filesaver.abort = function() { |
| 211 | + writer.abort(); |
| 212 | + filesaver.readyState = filesaver.DONE; |
| 213 | + }; |
| 214 | + filesaver.readyState = filesaver.WRITING; |
| 215 | + }), fs_error); |
| 216 | + }), fs_error); |
| 217 | + }; |
| 218 | + dir.getFile(name, {create: false}, abortable(function(file) { |
| 219 | + // delete file if it already exists |
| 220 | + file.remove(); |
| 221 | + save(); |
| 222 | + }), abortable(function(ex) { |
| 223 | + if (ex.code === ex.NOT_FOUND_ERR) { |
| 224 | + save(); |
| 225 | + } else { |
| 226 | + fs_error(); |
| 227 | + } |
| 228 | + })); |
| 229 | + }), fs_error); |
| 230 | + }), fs_error); |
| 231 | + } |
| 232 | + , FS_proto = FileSaver.prototype |
| 233 | + , saveAs = function(blob, name, no_auto_bom) { |
| 234 | + return new FileSaver(blob, name, no_auto_bom); |
| 235 | + } |
| 236 | + ; |
| 237 | + // IE 10+ (native saveAs) |
| 238 | + if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) { |
| 239 | + return function(blob, name, no_auto_bom) { |
| 240 | + if (!no_auto_bom) { |
| 241 | + blob = auto_bom(blob); |
| 242 | + } |
| 243 | + return navigator.msSaveOrOpenBlob(blob, name || "download"); |
| 244 | + }; |
| 245 | + } |
| 246 | + |
| 247 | + FS_proto.abort = function() { |
| 248 | + var filesaver = this; |
| 249 | + filesaver.readyState = filesaver.DONE; |
| 250 | + dispatch(filesaver, "abort"); |
| 251 | + }; |
| 252 | + FS_proto.readyState = FS_proto.INIT = 0; |
| 253 | + FS_proto.WRITING = 1; |
| 254 | + FS_proto.DONE = 2; |
| 255 | + |
| 256 | + FS_proto.error = |
| 257 | + FS_proto.onwritestart = |
| 258 | + FS_proto.onprogress = |
| 259 | + FS_proto.onwrite = |
| 260 | + FS_proto.onabort = |
| 261 | + FS_proto.onerror = |
| 262 | + FS_proto.onwriteend = |
| 263 | + null; |
| 264 | + |
| 265 | + return saveAs; |
| 266 | +}( |
| 267 | + typeof self !== "undefined" && self |
| 268 | + || typeof window !== "undefined" && window |
| 269 | + || this.content |
| 270 | +)); |
| 271 | +// `self` is undefined in Firefox for Android content script context |
| 272 | +// while `this` is nsIContentFrameMessageManager |
| 273 | +// with an attribute `content` that corresponds to the window |
| 274 | + |
| 275 | +if (typeof module !== "undefined" && module.exports) { |
| 276 | + module.exports.saveAs = saveAs; |
| 277 | +} else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) { |
| 278 | + define([], function() { |
| 279 | + return saveAs; |
| 280 | + }); |
| 281 | +} |
0 commit comments