1//download.js v4.2, by dandavis; 2008-2016. [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. 3.1 improved safari handling. 5// v4 adds AMD/UMD, commonJS, and plain browser support 6// v4.1 adds url download capability via solo URL argument (same domain/CORS only) 7// v4.2 adds semantic variable names, long (over 2MB) dataURL support, and hidden by default temp anchors 8// https://github.com/rndme/download 9 10(function (root, factory) { 11 if (typeof define === 'function' && define.amd) { 12 // AMD. Register as an anonymous module. 13 define([], factory); 14 } else if (typeof exports === 'object') { 15 // Node. Does not work with strict CommonJS, but 16 // only CommonJS-like environments that support module.exports, 17 // like Node. 18 module.exports = factory(); 19 } else { 20 // Browser globals (root is window) 21 root.download = factory(); 22 } 23}(this, function () { 24 25 return function download(data, strFileName, strMimeType) { 26 27 var self = window, // this script is only for browsers anyway... 28 defaultMime = "application/octet-stream", // this default mime also triggers iframe downloads 29 mimeType = strMimeType || defaultMime, 30 payload = data, 31 url = !strFileName && !strMimeType && payload, 32 anchor = document.createElement("a"), 33 toString = function(a){return String(a);}, 34 myBlob = (self.Blob || self.MozBlob || self.WebKitBlob || toString), 35 fileName = strFileName || "download", 36 blob, 37 reader; 38 myBlob= myBlob.call ? myBlob.bind(self) : Blob ; 39 40 if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback 41 payload=[payload, mimeType]; 42 mimeType=payload[0]; 43 payload=payload[1]; 44 } 45 46 47 if(url && url.length< 2048){ // if no filename and no mime, assume a url was passed as the only argument 48 fileName = url.split("/").pop().split("?")[0]; 49 anchor.href = url; // assign href prop to temp anchor 50 if(anchor.href.indexOf(url) !== -1){ // if the browser determines that it's a potentially valid url path: 51 var ajax=new XMLHttpRequest(); 52 ajax.open( "GET", url, true); 53 ajax.responseType = 'blob'; 54 ajax.onload= function(e){ 55 download(e.target.response, fileName, defaultMime); 56 }; 57 setTimeout(function(){ ajax.send();}, 0); // allows setting custom ajax headers using the return: 58 return ajax; 59 } // end if valid url? 60 } // end if url? 61 62 63 //go ahead and download dataURLs right away 64 if(/^data\:[\w+\-]+\/[\w+\-]+[,;]/.test(payload)){ 65 66 if(payload.length > (1024*1024*1.999) && myBlob !== toString ){ 67 payload=dataUrlToBlob(payload); 68 mimeType=payload.type || defaultMime; 69 }else{ 70 return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs: 71 navigator.msSaveBlob(dataUrlToBlob(payload), fileName) : 72 saver(payload) ; // everyone else can save dataURLs un-processed 73 } 74 75 }//end if dataURL passed? 76 77 blob = payload instanceof myBlob ? 78 payload : 79 new myBlob([payload], {type: mimeType}) ; 80 81 82 function dataUrlToBlob(strUrl) { 83 // PATCH GAYA : https://github.com/rndme/download/pull/93 84 var firstComma= strUrl.indexOf(','), 85 meta= strUrl.substring(5, firstComma).split(';'), 86 type= meta[0] || 'text/plain', 87 decoder= meta.indexOf("base64") !== -1 ? atob : decodeURIComponent, 88 binData= decoder( strUrl.substring(firstComma + 1) ), 89 mx= binData.length, 90 i= 0, 91 uiArr= new Uint8Array(mx); 92 93 for(i;i<mx;++i) uiArr[i]= binData.charCodeAt(i); 94 95 return new myBlob([uiArr], {type: type}); 96 } 97 98 function saver(url, winMode){ 99 100 if ('download' in anchor) { //html5 A[download] 101 anchor.href = url; 102 anchor.setAttribute("download", fileName); 103 anchor.className = "download-js-link"; 104 anchor.innerHTML = "downloading..."; 105 anchor.style.display = "none"; 106 document.body.appendChild(anchor); 107 setTimeout(function() { 108 anchor.click(); 109 document.body.removeChild(anchor); 110 if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(anchor.href);}, 250 );} 111 }, 66); 112 return true; 113 } 114 115 // handle non-a[download] safari as best we can: 116 if(/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//.test(navigator.userAgent)) { 117 url=url.replace(/^data:([\w\/\-\+]+)/, defaultMime); 118 if(!window.open(url)){ // popup blocked, offer direct download: 119 if(confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")){ location.href=url; } 120 } 121 return true; 122 } 123 124 //do iframe dataURL download (old ch+FF): 125 var f = document.createElement("iframe"); 126 document.body.appendChild(f); 127 128 if(!winMode
vendor: 963 bytes, lines 128-164
128){ // force a mime that will download: 129 url="data:"+url.replace(/^data:([\w\/\-\+]+)/, defaultMime); 130 } 131 f.src=url; 132 setTimeout(function(){ document.body.removeChild(f); }, 333); 133 134 }//end saver 135 136 137 138 139 if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL) 140 return navigator.msSaveBlob(blob, fileName); 141 } 142 143 if(self.URL){ // simple fast and modern way using Blob and URL: 144 saver(self.URL.createObjectURL(blob), true); 145 }else{ 146 // handle non-Blob()+non-URL browsers: 147 if(typeof blob === "string" || blob.constructor===toString ){ 148 try{ 149 return saver( "data:" + mimeType + ";base64," + self.btoa(blob) ); 150 }catch(y){ 151 return saver( "data:" + mimeType + "," + encodeURIComponent(blob) ); 152 } 153 } 154 155 // Blob but not URL support: 156 reader=new FileReader(); 157 reader.onload=function(e){ 158 saver(this.result); 159 }; 160 reader.readAsDataURL(blob); 161 } 162 return true; 163 }; /* end download() */ 164}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.