download2.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //download.js v3.0, by dandavis; 2008-2014. [CCBY2] see http://danml.com/download.html for tests/usage
  2. // v1 landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime
  3. // v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs
  4. // v3 added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support
  5. // data can be a string, Blob, File, or dataURL
  6. function download(data, strFileName, strMimeType) {
  7. var self = window, // this script is only for browsers anyway...
  8. u = "application/octet-stream", // this default mime also triggers iframe downloads
  9. m = strMimeType || u,
  10. x = data,
  11. D = document,
  12. a = D.createElement("a"),
  13. z = function(a){return String(a);},
  14. B = self.Blob || self.MozBlob || self.WebKitBlob || z,
  15. BB = self.MSBlobBuilder || self.WebKitBlobBuilder || self.BlobBuilder,
  16. fn = strFileName || "download",
  17. blob,
  18. b,
  19. ua,
  20. fr;
  21. //if(typeof B.bind === 'function' ){ B=B.bind(self); }
  22. if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
  23. x=[x, m];
  24. m=x[0];
  25. x=x[1];
  26. }
  27. //go ahead and download dataURLs right away
  28. if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)){
  29. return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs:
  30. navigator.msSaveBlob(d2b(x), fn) :
  31. saver(x) ; // everyone else can save dataURLs un-processed
  32. }//end if dataURL passed?
  33. try{
  34. blob = x instanceof B ?
  35. x :
  36. new B([x], {type: m}) ;
  37. }catch(y){
  38. if(BB){
  39. b = new BB();
  40. b.append([x]);
  41. blob = b.getBlob(m); // the blob
  42. }
  43. }
  44. function d2b(u) {
  45. var p= u.split(/[:;,]/),
  46. t= p[1],
  47. dec= p[2] == "base64" ? atob : decodeURIComponent,
  48. bin= dec(p.pop()),
  49. mx= bin.length,
  50. i= 0,
  51. uia= new Uint8Array(mx);
  52. for(i;i<mx;++i) uia[i]= bin.charCodeAt(i);
  53. return new B([uia], {type: t});
  54. }
  55. function saver(url, winMode){
  56. if ('download' in a) { //html5 A[download]
  57. a.href = url;
  58. a.setAttribute("download", fn);
  59. a.innerHTML = "downloading...";
  60. D.body.appendChild(a);
  61. setTimeout(function() {
  62. a.click();
  63. D.body.removeChild(a);
  64. if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(a.href);}, 250 );}
  65. }, 66);
  66. return true;
  67. }
  68. //do iframe dataURL download (old ch+FF):
  69. var f = D.createElement("iframe");
  70. D.body.appendChild(f);
  71. if(!winMode){ // force a mime that will download:
  72. url="data:"+url.replace(/^data:([\w\/\-\+]+)/, u);
  73. }
  74. f.src = url;
  75. setTimeout(function(){ D.body.removeChild(f); }, 333);
  76. }//end saver
  77. if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
  78. return navigator.msSaveBlob(blob, fn);
  79. }
  80. if(self.URL){ // simple fast and modern way using Blob and URL:
  81. saver(self.URL.createObjectURL(blob), true);
  82. }else{
  83. // handle non-Blob()+non-URL browsers:
  84. if(typeof blob === "string" || blob.constructor===z ){
  85. try{
  86. return saver( "data:" + m + ";base64," + self.btoa(blob) );
  87. }catch(y){
  88. return saver( "data:" + m + "," + encodeURIComponent(blob) );
  89. }
  90. }
  91. // Blob but not URL:
  92. fr=new FileReader();
  93. fr.onload=function(e){
  94. saver(this.result);
  95. };
  96. fr.readAsDataURL(blob);
  97. }
  98. return true;
  99. } /* end download() */