JS based on Filesaver.js plug-in implementation of the file Save function example [original]_javascript tips

Source: Internet
Author: User

This article describes the JS based on Filesaver.js plug-ins to implement file Save function. Share to everyone for your reference, specific as follows:

Here to introduce a JS plug-in usage is very simple, first look at the Filesaver.js plug-in source code:

Copy Code code as follows:

/*! @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=/version\/[\d\.] +.*safari/.test (navigator.useragent), f=e.webkitrequestfilesystem,u=e.requestfilesystem| | f| | E.mozrequestfilesystem,s=function (t) {(e.setimmediate| | E.settimeout) (function () {throw t},0)},c= "Application/octet-stream", D=0,l=500,w=function (t) {var r=function () {if ( typeof t=== "string") {n (). Revokeobjecturl (t)}else{t.remove ()}};if (e.chrome) {R ()}else{settimeout (r,l)}},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) {s (o)}}}},v=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},y=function (t,s,l) {if (!l) {t=v (t)}var y=this,m=t.type,s=false,h,r,o=function () {p (y) Writestart Progress write Writeend ". Split ("))},g=function () {if (r&&a&&typeof filereader!== "undefined") {var r= New Filereader;r.onloadend=function () {var e=r.result; r.location.href= "Data:attachment/file" +e.slice (E.search (/[,;] /)); Y.readystate=y.done;o ()};r.readasdataurl (t); Y.readystate=y.init;return}if (s| |! h) {h=n (). Createobjecturl (t)}if (R) {R.location.href=h}else{var I=e.open (h, "_blank"); if (I==undefined&&a) { E.location.href=h}}y.readystate=y.done;o (); W (h)},b=function (e) {return function () {if (Y.readystate!==y.done) { Return e.apply (this,arguments)}}},e={create:true,exclusive:false},n;y.readystate=y.init;if (!s) {s= "Download"}if ( i) {h=n (). Createobjecturl (t); R.href=h;r.download=s;settimeout (function () {O (r); O (); W (h); y.readystate=y.done}); Return}if (e.chrome&&m&&m!==c) {n=t.slice| | T.webkitslice;t=n.call (T,0,T.SIZE,C); S=true}if (f&&s!== "Download") {s+= ". Download"}iF (m===c| | f) {r=e}if (!u) {g (); Return}d+=t.size;u (E.temporary,d,b (function (e) {e.root.getdirectory ("saved", E,b (function (e) { var n=function () {E.getfile s,e,b (function (e) {E.createwriter (b (function (n) {n.onwriteend=function (t) { R.location.href=e.tourl (); Y.readystate=y.done;p (y, "writeend", T); W (e)};n.onerror=function () {var e=n.error;if ( E.code!==e.abort_err) {g ()}}; Writestart Progress Write Abort ". Split (" "). ForEach (function (e) {n[" on "+e]=y[" on "+e]}); N.write (t); Y.abort=function ( ) {N.abort (); y.readystate=y.done};y.readystate=y.writing}), G)}), G)};e.getfile (S,{create:false},b (function (e) { E.remove (); n ()}), B (function (e) {if (E.code===e.not_found_err) {n ()}else{g ()})}), (g)}), G)},m=y.prototype,s= function (e,t,n) {return new Y (e,t,n)};if (typeof navigator!== "undefined" &&navigator.mssaveoropenblob) { return function (e,t,n) {if (!n) {e=v (E)}return Navigator.mssaveoropenblob (e,t| |) Download ")}}m.abort=function () {var e=this;e.readystate=e.done;p (E," abort ")};m.readystate=m.init=0;m.writing=1;m . Done=2;m.error=m.onwritEstart=m.onprogress=m.onwrite=m.onabort=m.onerror=m.onwriteend=null;return S} (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 })}

The source code readability dare not compliment, let us use this station JS online tool:http://tools.jb51.net/code/js format, convenient reading research later.

The formatted code is as follows:

/*!
@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.doc ument, 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 =/version\/[\d\.] +.*safari/.test (navigator.useragent), f = e.webkitrequestfilesystem, U = E.requestfilesystem | | f | | 
  E.mozrequestfilesystem, s = function (t) {(E.setimmediate | | e.settimeout) (function () {throw T}, 0)},  c = "Application/octet-stream", d = 0, L = +, W = function (t) {var r = function () {if (typeof t = =
    ' String ' {n (). Revokeobjecturl (t)} else {T.remove ()}}; if (e.chrome) {R ()}else {settimeout (R, L)}}, 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) {s (o)} }}, V = 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})} Retu
    RN e}, y = function (t, S, l) {if (!l) {t = V (t)} var y = this, M = T.type, S = False, H, R, O = function () {p (Y, "writestart Progress Write Writeend". Split (""))}, G = function () {if (R && a && typeof filereader!== "undefined")
        {var r = new FileReader;
          R.onloadend = function () {var e = R.result; R.location.href = "Data:attachment/file" + E.slice (E.search (/[,;]
          /));Y.readystate = Y.done;
        O ()};
        R.readasdataurl (t);
        Y.readystate = Y.init;
      return} if (S | |!h) {h = n (). Createobjecturl (t)} if (R) {r.location.href = h
        else {var i = E.open (h, "_blank");
      if (i = = undefined && a) {e.location.href = h}} y.readystate = Y.done;
      O (); W (h)}, B = function (e) {return function () {if (y.readystate!== y.done) {return E.APPL
    Y (this, arguments)}}}, E = {create:true, exclusive:false}, N;
    Y.readystate = Y.init;
      if (!s) {s = "Download"} if (i) {h = n (). Createobjecturl (t);
      R.href = h;
      R.download = s;
        settimeout (function () {O (R);
        O ();
        W (h);
      Y.readystate = Y.done});
    return} if (E.chrome && m && m!== c) {  N = T.slice | |
      T.webkitslice;
      t = N.call (t, 0, T.size, c);
    s = true} if (f && s!== "Download") {s + = ". Download"} if (m = = c | | f) {R = E
      } if (!u) {g ();
    return} D + = T.size;
          U (e.temporary, d, B (function (e) {e.root.getdirectory ("saved", E, B (function (e) {var n = function () { 
                E.getfile (S, E, B (function (E) {E.createwriter (b) (function (n) {n.onwriteend = function (t) {
                R.location.href = E.tourl ();
                Y.readystate = Y.done;
                P (Y, "writeend", t);
              W (e)};
                N.onerror = function () {var e = N.error;
              if (E.code!== e.abort_err) {g ()}};
              "Writestart Progress Write Abort". Split (""). ForEach (function (e) {n["" + e] = y["on" + E]
              }); N. Write (t);
                Y.abort = function () {n.abort ();
              Y.readystate = Y.done};
        Y.readystate = y.writing}) (g)}), g)};
          E.getfile (S, {create:false}, B (function (e) {e.remove ();
            N ()}), B (function (e) {if (E.code = = E.not_found_err) {n ()} else {  g ()})) (g)}) (g)}, M = y.prototype, S = function (e, T, N) {return new Y (E,
  T, N)};  if (typeof navigator!== "undefined" && navigator.mssaveoropenblob) {return function (E, T, N) {if (!n) {e = V (e)} return Navigator.mssaveoropenblob (E, T | |
    ' Download '}} m.abort = function () {var e = this;
    E.readystate = E.done;
  P (E, "Abort")};
  m.readystate = M.init = 0;
  m.writing = 1;
  M.done = 2; M.error = M.onwritestart = M.onprogreSS = M.onwrite = M.onabort = M.onerror = M.onwriteend = null; Return S} (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

 })
}

It's very simple to use, as shown in the following code:

var blob = new blob ([' out_put_string '], {type: ' Text/plain;charset=utf-8 '}),//out_put_string string content
to be saved to file SaveAs (BLOB, "filename.php");//filename.php for saved file name

PS: This site online tool "PHP code online format Beautification tool" in the Save text function is Filesaver.js plug-in implementation. Interested friends can refer to:

Http://tools.jb51.net/code/phpformat

More readers interested in JavaScript-related content can view the site topics: "JavaScript Expansion Tips Summary", "JavaScript file and Directory operation tips Summary", "JavaScript Error and debugging skills summary", " JavaScript data structure and algorithm skills summary, "javascript traversal algorithm and Skills summary" and "JavaScript Mathematical Computing Usage Summary"

I hope this article will help you with JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.