Files
FileSaver.js/FileSaver.min.js
T
2015-10-03 23:26:56 +03:00

2 lines
3.3 KiB
JavaScript

/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs=saveAs||function(e){"use strict";if(typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),i="download"in r,o=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=e.webkitRequestFileSystem,f=e.requestFileSystem||a||e.mozRequestFileSystem,u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},s="application/octet-stream",c=0,d=500,l=function(t){var r=function(){if(typeof t==="string"){n().revokeObjectURL(t)}else{t.remove()}};if(e.chrome){r()}else{setTimeout(r,d)}},p=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var i=e["on"+t[r]];if(typeof i==="function"){try{i.call(e,n||e)}catch(o){u(o)}}}},w=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob(["\ufeff",e],{type:e.type})}return e},v=function(t,u,d){if(!d){t=w(t)}var v=this,y=t.type,m=false,S,h,R=function(){p(v,"writestart progress write writeend".split(" "))},O=function(){if(h&&/Version\/[\d\.]+.*Safari/.test(navigator.userAgent)&&typeof FileReader!=="undefined"){var r=new FileReader;r.onloadend=function(){var e=r.result;e=e.replace(/^data:.*?;/,"data:attachment/file;");h.location.href=e;v.readyState=v.DONE;R()};r.readAsDataURL(t);v.readyState=v.INIT;return}if(m||!S){S=n().createObjectURL(t)}if(h){h.location.href=S}else{var i=e.open(S,"_blank");if(i==undefined&&typeof safari!=="undefined"){e.location.href=S}}v.readyState=v.DONE;R();l(S)},g=function(e){return function(){if(v.readyState!==v.DONE){return e.apply(this,arguments)}}},b={create:true,exclusive:false},E;v.readyState=v.INIT;if(!u){u="download"}if(i){S=n().createObjectURL(t);r.href=S;r.download=u;setTimeout(function(){o(r);R();l(S);v.readyState=v.DONE});return}if(e.chrome&&y&&y!==s){E=t.slice||t.webkitSlice;t=E.call(t,0,t.size,s);m=true}if(a&&u!=="download"){u+=".download"}if(y===s||a){h=e}if(!f){O();return}c+=t.size;f(e.TEMPORARY,c,g(function(e){e.root.getDirectory("saved",b,g(function(e){var n=function(){e.getFile(u,b,g(function(e){e.createWriter(g(function(n){n.onwriteend=function(t){h.location.href=e.toURL();v.readyState=v.DONE;p(v,"writeend",t);l(e)};n.onerror=function(){var e=n.error;if(e.code!==e.ABORT_ERR){O()}};"writestart progress write abort".split(" ").forEach(function(e){n["on"+e]=v["on"+e]});n.write(t);v.abort=function(){n.abort();v.readyState=v.DONE};v.readyState=v.WRITING}),O)}),O)};e.getFile(u,{create:false},g(function(e){e.remove();n()}),g(function(e){if(e.code===e.NOT_FOUND_ERR){n()}else{O()}}))}),O)}),O)},y=v.prototype,m=function(e,t,n){return new v(e,t,n)};if(typeof navigator!=="undefined"&&navigator.msSaveOrOpenBlob){return function(e,t,n){if(!n){e=w(e)}return navigator.msSaveOrOpenBlob(e,t||"download")}}y.abort=function(){var e=this;e.readyState=e.DONE;p(e,"abort")};y.readyState=y.INIT=0;y.WRITING=1;y.DONE=2;y.error=y.onwritestart=y.onprogress=y.onwrite=y.onabort=y.onerror=y.onwriteend=null;return m}(typeof self!=="undefined"&&self||typeof window!=="undefined"&&window||this.content);if(typeof module!=="undefined"&&module.exports){module.exports.saveAs=saveAs}else if(typeof define!=="undefined"&&define!==null&&define.amd!=null){define([],function(){return saveAs})}