//download.js v3.0, by dandavis; 2008-2014. [CCBY2] see http://danml.com/download.html for tests/usage // v1 landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime // v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs // v3 added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support // data can be a string, Blob, File, or dataURL function download(data, strFileName, strMimeType) { var self = window, // this script is only for browsers anyway... u = "application/octet-stream", // this default mime also triggers iframe downloads m = strMimeType || u, x = data, D = document, a = D.createElement("a"), z = function(a){return String(a);}, B = self.Blob || self.MozBlob || self.WebKitBlob || z, BB = self.MSBlobBuilder || self.WebKitBlobBuilder || self.BlobBuilder, fn = strFileName || "download", blob, b, ua, fr; //if(typeof B.bind === 'function' ){ B=B.bind(self); } if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback x=[x, m]; m=x[0]; x=x[1]; } //go ahead and download dataURLs right away if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)){ return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs: navigator.msSaveBlob(d2b(x), fn) : saver(x) ; // everyone else can save dataURLs un-processed }//end if dataURL passed? try{ blob = x instanceof B ? x : new B([x], {type: m}) ; }catch(y){ if(BB){ b = new BB(); b.append([x]); blob = b.getBlob(m); // the blob } } function d2b(u) { var p= u.split(/[:;,]/), t= p[1], dec= p[2] == "base64" ? atob : decodeURIComponent, bin= dec(p.pop()), mx= bin.length, i= 0, uia= new Uint8Array(mx); for(i;i