filesaver.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* Blob.js
  2. * A Blob implementation.
  3. * 2013-06-20
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. * By Devin Samarin, https://github.com/eboyjr
  7. * License: X11/MIT
  8. * See LICENSE.md
  9. */
  10. /*global self, unescape */
  11. /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
  12. plusplus: true */
  13. /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
  14. if (!(typeof Blob === "function" || typeof Blob === "object") || typeof URL === "undefined")
  15. if ((typeof Blob === "function" || typeof Blob === "object") && typeof webkitURL !== "undefined") self.URL = webkitURL;
  16. else var Blob = (function (view) {
  17. "use strict";
  18. var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || view.MSBlobBuilder || (function(view) {
  19. var
  20. get_class = function(object) {
  21. return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
  22. }
  23. , FakeBlobBuilder = function BlobBuilder() {
  24. this.data = [];
  25. }
  26. , FakeBlob = function Blob(data, type, encoding) {
  27. this.data = data;
  28. this.size = data.length;
  29. this.type = type;
  30. this.encoding = encoding;
  31. }
  32. , FBB_proto = FakeBlobBuilder.prototype
  33. , FB_proto = FakeBlob.prototype
  34. , FileReaderSync = view.FileReaderSync
  35. , FileException = function(type) {
  36. this.code = this[this.name = type];
  37. }
  38. , file_ex_codes = (
  39. "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
  40. + "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
  41. ).split(" ")
  42. , file_ex_code = file_ex_codes.length
  43. , real_URL = view.URL || view.webkitURL || view
  44. , real_create_object_URL = real_URL.createObjectURL
  45. , real_revoke_object_URL = real_URL.revokeObjectURL
  46. , URL = real_URL
  47. , btoa = view.btoa
  48. , atob = view.atob
  49. , ArrayBuffer = view.ArrayBuffer
  50. , Uint8Array = view.Uint8Array
  51. ;
  52. FakeBlob.fake = FB_proto.fake = true;
  53. while (file_ex_code--) {
  54. FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
  55. }
  56. if (!real_URL.createObjectURL) {
  57. URL = view.URL = {};
  58. }
  59. URL.createObjectURL = function(blob) {
  60. var
  61. type = blob.type
  62. , data_URI_header
  63. ;
  64. if (type === null) {
  65. type = "application/octet-stream";
  66. }
  67. if (blob instanceof FakeBlob) {
  68. data_URI_header = "data:" + type;
  69. if (blob.encoding === "base64") {
  70. return data_URI_header + ";base64," + blob.data;
  71. } else if (blob.encoding === "URI") {
  72. return data_URI_header + "," + decodeURIComponent(blob.data);
  73. } if (btoa) {
  74. return data_URI_header + ";base64," + btoa(blob.data);
  75. } else {
  76. return data_URI_header + "," + encodeURIComponent(blob.data);
  77. }
  78. } else if (real_create_object_URL) {
  79. return real_create_object_URL.call(real_URL, blob);
  80. }
  81. };
  82. URL.revokeObjectURL = function(object_URL) {
  83. if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
  84. real_revoke_object_URL.call(real_URL, object_URL);
  85. }
  86. };
  87. FBB_proto.append = function(data/*, endings*/) {
  88. var bb = this.data;
  89. // decode data to a binary string
  90. if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
  91. var
  92. str = ""
  93. , buf = new Uint8Array(data)
  94. , i = 0
  95. , buf_len = buf.length
  96. ;
  97. for (; i < buf_len; i++) {
  98. str += String.fromCharCode(buf[i]);
  99. }
  100. bb.push(str);
  101. } else if (get_class(data) === "Blob" || get_class(data) === "File") {
  102. if (FileReaderSync) {
  103. var fr = new FileReaderSync;
  104. bb.push(fr.readAsBinaryString(data));
  105. } else {
  106. // async FileReader won't work as BlobBuilder is sync
  107. throw new FileException("NOT_READABLE_ERR");
  108. }
  109. } else if (data instanceof FakeBlob) {
  110. if (data.encoding === "base64" && atob) {
  111. bb.push(atob(data.data));
  112. } else if (data.encoding === "URI") {
  113. bb.push(decodeURIComponent(data.data));
  114. } else if (data.encoding === "raw") {
  115. bb.push(data.data);
  116. }
  117. } else {
  118. if (typeof data !== "string") {
  119. data += ""; // convert unsupported types to strings
  120. }
  121. // decode UTF-16 to binary string
  122. bb.push(unescape(encodeURIComponent(data)));
  123. }
  124. };
  125. FBB_proto.getBlob = function(type) {
  126. if (!arguments.length) {
  127. type = null;
  128. }
  129. return new FakeBlob(this.data.join(""), type, "raw");
  130. };
  131. FBB_proto.toString = function() {
  132. return "[object BlobBuilder]";
  133. };
  134. FB_proto.slice = function(start, end, type) {
  135. var args = arguments.length;
  136. if (args < 3) {
  137. type = null;
  138. }
  139. return new FakeBlob(
  140. this.data.slice(start, args > 1 ? end : this.data.length)
  141. , type
  142. , this.encoding
  143. );
  144. };
  145. FB_proto.toString = function() {
  146. return "[object Blob]";
  147. };
  148. return FakeBlobBuilder;
  149. }(view));
  150. return function Blob(blobParts, options) {
  151. var type = options ? (options.type || "") : "";
  152. var builder = new BlobBuilder();
  153. if (blobParts) {
  154. for (var i = 0, len = blobParts.length; i < len; i++) {
  155. builder.append(blobParts[i]);
  156. }
  157. }
  158. return builder.getBlob(type);
  159. };
  160. }(self));
  161. /* FileSaver.js
  162. * A saveAs() FileSaver implementation.
  163. * 2013-10-21
  164. *
  165. * By Eli Grey, http://eligrey.com
  166. * License: X11/MIT
  167. * See LICENSE.md
  168. */
  169. /*global self */
  170. /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
  171. plusplus: true */
  172. /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
  173. var saveAs = saveAs
  174. || (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
  175. || (function(view) {
  176. "use strict";
  177. var
  178. doc = view.document
  179. // only get URL when necessary in case BlobBuilder.js hasn't overridden it yet
  180. , get_URL = function() {
  181. return view.URL || view.webkitURL || view;
  182. }
  183. , URL = view.URL || view.webkitURL || view
  184. , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
  185. , can_use_save_link = !view.externalHost && "download" in save_link
  186. , click = function(node) {
  187. var event = doc.createEvent("MouseEvents");
  188. event.initMouseEvent(
  189. "click", true, false, view, 0, 0, 0, 0, 0
  190. , false, false, false, false, 0, null
  191. );
  192. node.dispatchEvent(event);
  193. }
  194. , webkit_req_fs = view.webkitRequestFileSystem
  195. , req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
  196. , throw_outside = function (ex) {
  197. (view.setImmediate || view.setTimeout)(function() {
  198. throw ex;
  199. }, 0);
  200. }
  201. , force_saveable_type = "application/octet-stream"
  202. , fs_min_size = 0
  203. , deletion_queue = []
  204. , process_deletion_queue = function() {
  205. var i = deletion_queue.length;
  206. while (i--) {
  207. var file = deletion_queue[i];
  208. if (typeof file === "string") { // file is an object URL
  209. URL.revokeObjectURL(file);
  210. } else { // file is a File
  211. file.remove();
  212. }
  213. }
  214. deletion_queue.length = 0; // clear queue
  215. }
  216. , dispatch = function(filesaver, event_types, event) {
  217. event_types = [].concat(event_types);
  218. var i = event_types.length;
  219. while (i--) {
  220. var listener = filesaver["on" + event_types[i]];
  221. if (typeof listener === "function") {
  222. try {
  223. listener.call(filesaver, event || filesaver);
  224. } catch (ex) {
  225. throw_outside(ex);
  226. }
  227. }
  228. }
  229. }
  230. , FileSaver = function(blob, name) {
  231. // First try a.download, then web filesystem, then object URLs
  232. var
  233. filesaver = this
  234. , type = blob.type
  235. , blob_changed = false
  236. , object_url
  237. , target_view
  238. , get_object_url = function() {
  239. var object_url = get_URL().createObjectURL(blob);
  240. deletion_queue.push(object_url);
  241. return object_url;
  242. }
  243. , dispatch_all = function() {
  244. dispatch(filesaver, "writestart progress write writeend".split(" "));
  245. }
  246. // on any filesys errors revert to saving with object URLs
  247. , fs_error = function() {
  248. // don't create more object URLs than needed
  249. if (blob_changed || !object_url) {
  250. object_url = get_object_url(blob);
  251. }
  252. if (target_view) {
  253. target_view.location.href = object_url;
  254. } else {
  255. window.open(object_url, "_blank");
  256. }
  257. filesaver.readyState = filesaver.DONE;
  258. dispatch_all();
  259. }
  260. , abortable = function(func) {
  261. return function() {
  262. if (filesaver.readyState !== filesaver.DONE) {
  263. return func.apply(this, arguments);
  264. }
  265. };
  266. }
  267. , create_if_not_found = {create: true, exclusive: false}
  268. , slice
  269. ;
  270. filesaver.readyState = filesaver.INIT;
  271. if (!name) {
  272. name = "download";
  273. }
  274. if (can_use_save_link) {
  275. object_url = get_object_url(blob);
  276. // FF for Android has a nasty garbage collection mechanism
  277. // that turns all objects that are not pure javascript into 'deadObject'
  278. // this means `doc` and `save_link` are unusable and need to be recreated
  279. // `view` is usable though:
  280. doc = view.document;
  281. save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a");
  282. save_link.href = object_url;
  283. save_link.download = name;
  284. var event = doc.createEvent("MouseEvents");
  285. event.initMouseEvent(
  286. "click", true, false, view, 0, 0, 0, 0, 0
  287. , false, false, false, false, 0, null
  288. );
  289. save_link.dispatchEvent(event);
  290. filesaver.readyState = filesaver.DONE;
  291. dispatch_all();
  292. return;
  293. }
  294. // Object and web filesystem URLs have a problem saving in Google Chrome when
  295. // viewed in a tab, so I force save with application/octet-stream
  296. // http://code.google.com/p/chromium/issues/detail?id=91158
  297. if (view.chrome && type && type !== force_saveable_type) {
  298. slice = blob.slice || blob.webkitSlice;
  299. blob = slice.call(blob, 0, blob.size, force_saveable_type);
  300. blob_changed = true;
  301. }
  302. // Since I can't be sure that the guessed media type will trigger a download
  303. // in WebKit, I append .download to the filename.
  304. // https://bugs.webkit.org/show_bug.cgi?id=65440
  305. if (webkit_req_fs && name !== "download") {
  306. name += ".download";
  307. }
  308. if (type === force_saveable_type || webkit_req_fs) {
  309. target_view = view;
  310. }
  311. if (!req_fs) {
  312. fs_error();
  313. return;
  314. }
  315. fs_min_size += blob.size;
  316. req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) {
  317. fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) {
  318. var save = function() {
  319. dir.getFile(name, create_if_not_found, abortable(function(file) {
  320. file.createWriter(abortable(function(writer) {
  321. writer.onwriteend = function(event) {
  322. target_view.location.href = file.toURL();
  323. deletion_queue.push(file);
  324. filesaver.readyState = filesaver.DONE;
  325. dispatch(filesaver, "writeend", event);
  326. };
  327. writer.onerror = function() {
  328. var error = writer.error;
  329. if (error.code !== error.ABORT_ERR) {
  330. fs_error();
  331. }
  332. };
  333. "writestart progress write abort".split(" ").forEach(function(event) {
  334. writer["on" + event] = filesaver["on" + event];
  335. });
  336. writer.write(blob);
  337. filesaver.abort = function() {
  338. writer.abort();
  339. filesaver.readyState = filesaver.DONE;
  340. };
  341. filesaver.readyState = filesaver.WRITING;
  342. }), fs_error);
  343. }), fs_error);
  344. };
  345. dir.getFile(name, {create: false}, abortable(function(file) {
  346. // delete file if it already exists
  347. file.remove();
  348. save();
  349. }), abortable(function(ex) {
  350. if (ex.code === ex.NOT_FOUND_ERR) {
  351. save();
  352. } else {
  353. fs_error();
  354. }
  355. }));
  356. }), fs_error);
  357. }), fs_error);
  358. }
  359. , FS_proto = FileSaver.prototype
  360. , saveAs = function(blob, name) {
  361. return new FileSaver(blob, name);
  362. }
  363. ;
  364. FS_proto.abort = function() {
  365. var filesaver = this;
  366. filesaver.readyState = filesaver.DONE;
  367. dispatch(filesaver, "abort");
  368. };
  369. FS_proto.readyState = FS_proto.INIT = 0;
  370. FS_proto.WRITING = 1;
  371. FS_proto.DONE = 2;
  372. FS_proto.error =
  373. FS_proto.onwritestart =
  374. FS_proto.onprogress =
  375. FS_proto.onwrite =
  376. FS_proto.onabort =
  377. FS_proto.onerror =
  378. FS_proto.onwriteend =
  379. null;
  380. view.addEventListener("unload", process_deletion_queue, false);
  381. return saveAs;
  382. }(this.self || this.window || this.content));
  383. // `self` is undefined in Firefox for Android content script context
  384. // while `this` is nsIContentFrameMessageManager
  385. // with an attribute `content` that corresponds to the window
  386. if (typeof module !== 'undefined') module.exports = saveAs;