Comparing version 1.4.2 to 1.4.3
130
download.js
@@ -1,3 +0,2 @@ | ||
//download.js v4.1, by dandavis; 2008-2015. [CCBY2] see http://danml.com/download.html for tests/usage | ||
//download.js v4.2, by dandavis; 2008-2016. [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 | ||
@@ -8,2 +7,3 @@ // v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs | ||
// v4.1 adds url download capability via solo URL argument (same domain/CORS only) | ||
// v4.2 adds semantic variable names, long (over 2MB) dataURL support, and hidden by default temp anchors | ||
// https://github.com/rndme/download | ||
@@ -29,28 +29,25 @@ | ||
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, | ||
url = !strFileName && !strMimeType && x, | ||
D = document, | ||
a = D.createElement("a"), | ||
z = function(a){return String(a);}, | ||
B = (self.Blob || self.MozBlob || self.WebKitBlob || z), | ||
fn = strFileName || "download", | ||
defaultMime = "application/octet-stream", // this default mime also triggers iframe downloads | ||
mimeType = strMimeType || defaultMime, | ||
payload = data, | ||
url = !strFileName && !strMimeType && payload, | ||
anchor = document.createElement("a"), | ||
toString = function(a){return String(a);}, | ||
myBlob = (self.Blob || self.MozBlob || self.WebKitBlob || toString), | ||
fileName = strFileName || "download", | ||
blob, | ||
fr, | ||
ajax; | ||
B= B.call ? B.bind(self) : Blob ; | ||
reader; | ||
myBlob= myBlob.call ? myBlob.bind(self) : Blob ; | ||
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]; | ||
payload=[payload, mimeType]; | ||
mimeType=payload[0]; | ||
payload=payload[1]; | ||
} | ||
if(url && url.length< 2048){ | ||
fn = url.split("/").pop().split("?")[0]; | ||
a.href = url; // assign href prop to temp anchor | ||
if(a.href.indexOf(url) !== -1){ // if the browser determines that it's a potentially valid url path: | ||
if(url && url.length< 2048){ // if no filename and no mime, assume a url was passed as the only argument | ||
fileName = url.split("/").pop().split("?")[0]; | ||
anchor.href = url; // assign href prop to temp anchor | ||
if(anchor.href.indexOf(url) !== -1){ // if the browser determines that it's a potentially valid url path: | ||
var ajax=new XMLHttpRequest(); | ||
@@ -60,5 +57,5 @@ ajax.open( "GET", url, true); | ||
ajax.onload= function(e){ | ||
download(e.target.response, fn, u); | ||
download(e.target.response, fileName, defaultMime); | ||
}; | ||
ajax.send(); | ||
setTimeout(function(){ ajax.send();}, 0); // allows setting custom ajax headers using the return: | ||
return ajax; | ||
@@ -69,27 +66,33 @@ } // end if valid url? | ||
//go ahead and download dataURLs right away | ||
if(/^data\:[\w+\-]+\/[\w+\-]+[,;]/.test(x)){ | ||
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 | ||
if(/^data\:[\w+\-]+\/[\w+\-]+[,;]/.test(payload)){ | ||
if(payload.length > (1024*1024*1.999) && myBlob !== toString ){ | ||
payload=dataUrlToBlob(payload); | ||
mimeType=payload.type || defaultMime; | ||
}else{ | ||
return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs: | ||
navigator.msSaveBlob(dataUrlToBlob(payload), fileName) : | ||
saver(payload) ; // everyone else can save dataURLs un-processed | ||
} | ||
}//end if dataURL passed? | ||
blob = x instanceof B ? | ||
x : | ||
new B([x], {type: m}) ; | ||
blob = payload instanceof myBlob ? | ||
payload : | ||
new myBlob([payload], {type: mimeType}) ; | ||
function d2b(u) { | ||
var p= u.split(/[:;,]/), | ||
t= p[1], | ||
dec= p[2] == "base64" ? atob : decodeURIComponent, | ||
bin= dec(p.pop()), | ||
mx= bin.length, | ||
function dataUrlToBlob(strUrl) { | ||
var parts= strUrl.split(/[:;,]/), | ||
type= parts[1], | ||
decoder= parts[2] == "base64" ? atob : decodeURIComponent, | ||
binData= decoder( parts.pop() ), | ||
mx= binData.length, | ||
i= 0, | ||
uia= new Uint8Array(mx); | ||
uiArr= new Uint8Array(mx); | ||
for(i;i<mx;++i) uia[i]= bin.charCodeAt(i); | ||
for(i;i<mx;++i) uiArr[i]= binData.charCodeAt(i); | ||
return new B([uia], {type: t}); | ||
return new myBlob([uiArr], {type: type}); | ||
} | ||
@@ -99,12 +102,13 @@ | ||
if ('download' in a) { //html5 A[download] | ||
a.href = url; | ||
a.setAttribute("download", fn); | ||
a.className = "download-js-link"; | ||
a.innerHTML = "downloading..."; | ||
D.body.appendChild(a); | ||
if ('download' in anchor) { //html5 A[download] | ||
anchor.href = url; | ||
anchor.setAttribute("download", fileName); | ||
anchor.className = "download-js-link"; | ||
anchor.innerHTML = "downloading..."; | ||
anchor.style.display = "none"; | ||
document.body.appendChild(anchor); | ||
setTimeout(function() { | ||
a.click(); | ||
D.body.removeChild(a); | ||
if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(a.href);}, 250 );} | ||
anchor.click(); | ||
document.body.removeChild(anchor); | ||
if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(anchor.href);}, 250 );} | ||
}, 66); | ||
@@ -116,3 +120,3 @@ return true; | ||
if(/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//.test(navigator.userAgent)) { | ||
url=url.replace(/^data:([\w\/\-\+]+)/, u); | ||
url=url.replace(/^data:([\w\/\-\+]+)/, defaultMime); | ||
if(!window.open(url)){ // popup blocked, offer direct download: | ||
@@ -125,10 +129,10 @@ if(confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")){ location.href=url; } | ||
//do iframe dataURL download (old ch+FF): | ||
var f = D.createElement("iframe"); | ||
D.body.appendChild(f); | ||
var f = document.createElement("iframe"); | ||
document.body.appendChild(f); | ||
if(!winMode){ // force a mime that will download: | ||
url="data:"+url.replace(/^data:([\w\/\-\+]+)/, u); | ||
url="data:"+url.replace(/^data:([\w\/\-\+]+)/, defaultMime); | ||
} | ||
f.src=url; | ||
setTimeout(function(){ D.body.removeChild(f); }, 333); | ||
setTimeout(function(){ document.body.removeChild(f); }, 333); | ||
@@ -141,3 +145,3 @@ }//end saver | ||
if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL) | ||
return navigator.msSaveBlob(blob, fn); | ||
return navigator.msSaveBlob(blob, fileName); | ||
} | ||
@@ -149,16 +153,16 @@ | ||
// handle non-Blob()+non-URL browsers: | ||
if(typeof blob === "string" || blob.constructor===z ){ | ||
if(typeof blob === "string" || blob.constructor===toString ){ | ||
try{ | ||
return saver( "data:" + m + ";base64," + self.btoa(blob) ); | ||
return saver( "data:" + mimeType + ";base64," + self.btoa(blob) ); | ||
}catch(y){ | ||
return saver( "data:" + m + "," + encodeURIComponent(blob) ); | ||
return saver( "data:" + mimeType + "," + encodeURIComponent(blob) ); | ||
} | ||
} | ||
// Blob but not URL: | ||
fr=new FileReader(); | ||
fr.onload=function(e){ | ||
// Blob but not URL support: | ||
reader=new FileReader(); | ||
reader.onload=function(e){ | ||
saver(this.result); | ||
}; | ||
fr.readAsDataURL(blob); | ||
reader.readAsDataURL(blob); | ||
} | ||
@@ -165,0 +169,0 @@ return true; |
{ | ||
"name": "downloadjs", | ||
"main": "download.js", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"description": "file downloading using client-side javascript", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -1,5 +0,5 @@ | ||
download | ||
# download | ||
======== | ||
Summary | ||
## Summary | ||
--------- | ||
@@ -10,7 +10,7 @@ The download() function is used to trigger a file download from JavaScript. | ||
Syntax | ||
## Getting and Using | ||
--------- | ||
#### Via NPM/Bower | ||
`npm install downloadjs` | ||
### Via NPM/Bower | ||
`npm install downloadjs` <br /> | ||
`bower install downloadjs` | ||
@@ -20,6 +20,6 @@ | ||
#### Simple global `download` function via `<script>` include | ||
### Simple global `download` function via `<script>` include | ||
download(data, strFileName, strMimeType); | ||
#### Included via AMD | ||
### Included via AMD | ||
require(['path/to/file'], function(download) { | ||
@@ -30,3 +30,3 @@ download(data, strFileName, strMimeType); | ||
Parameters | ||
### Parameters | ||
--------- | ||
@@ -39,3 +39,3 @@ * **data** - The Blob, File, String, or dataURL containing the soon-to-be File's contents. | ||
Example Usage | ||
## Example Usage | ||
--------- | ||
@@ -45,16 +45,16 @@ | ||
### Plain Text | ||
#### text string - [live demo](http://pagedemos.com/sxks39b72aqb/1) | ||
#### text string - [live demo](http://pagedemos.com/hw24em95rsfq/output/) | ||
download("hello world", "dlText.txt", "text/plain"); | ||
#### text dataURL - [live demo](http://pagedemos.com/sxks39b72aqb/2) | ||
#### text dataURL - [live demo](http://pagedemos.com/r9ywm98s6b29/output/) | ||
download("data:text/plain,hello%20world", "dlDataUrlText.txt", "text/plain"); | ||
#### text blob - [live demo](http://pagedemos.com/sxks39b72aqb/3) | ||
#### text blob - [live demo](http://pagedemos.com/ckcah2vp8kza/output/) | ||
download(new Blob(["hello world"]), "dlTextBlob.txt", "text/plain"); | ||
#### text url - [live demo](http://pagedemos.com/pz6hkyqutjtw/) | ||
#### text url - [live demo](http://pagedemos.com/pz6hkyqutjtw/output/) | ||
download("/robots.txt"); | ||
#### text UInt8 Array - [live demo](http://pagedemos.com/sxks39b72aqb/4) | ||
#### text UInt8 Array - [live demo](http://pagedemos.com/zuyk46wbkktq/output/) | ||
var str= "hello world", arr= new Uint8Array(str.length); | ||
@@ -68,9 +68,9 @@ str.split("").forEach(function(a,b){ | ||
### HTML | ||
#### html string - [live demo](http://pagedemos.com/sxks39b72aqb/5) | ||
download(document.body.outerHTML, "dlHTML.html", "text/html"); | ||
#### html string - [live demo](http://pagedemos.com/k7rwq7msu3eb/output/) | ||
download(document.documentElement.outerHTML, "dlHTML.html", "text/html"); | ||
#### html Blob - [live demo](http://pagedemos.com/sxks39b72aqb/6) | ||
#### html Blob - [live demo](http://pagedemos.com/bxehm2fdf3g4/output/) | ||
download(new Blob(["hello world".bold()]), "dlHtmlBlob.html", "text/html"); | ||
#### ajax callback - [live demo](http://pagedemos.com/sxks39b72aqb/7) | ||
#### ajax callback - [live demo](http://pagedemos.com/arr2ym74aw8t/output/) | ||
(note that callback mode won't work on vanilla ajax or with binary files) | ||
@@ -85,6 +85,6 @@ | ||
### Binary Files | ||
#### image from URL - [live demo](http://pagedemos.com/yvvmxbjrwq7u/) | ||
#### image from URL - [live demo](http://pagedemos.com/yvvmxbjrwq7u/output/) | ||
download("/diff6.png"); | ||
#### Image via ajax for custom filename - [live demo](http://pagedemos.com/v2848zfgwrju/) | ||
#### Image via ajax for custom filename - [live demo](http://pagedemos.com/v2848zfgwrju/output/) | ||
var x=new XMLHttpRequest(); | ||
@@ -97,3 +97,3 @@ x.open( "GET", "/diff6.png" , true); | ||
Compatibility | ||
## Compatibility | ||
--------- | ||
@@ -112,3 +112,3 @@ download.js works with a wide range of devices and browsers. | ||
FAQ | ||
## FAQ | ||
--------- | ||
@@ -123,3 +123,3 @@ | ||
Change Log (v4.1) | ||
## Change Log (v4.1) | ||
--------- | ||
@@ -131,2 +131,3 @@ * 2008 :: landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime | ||
* 2015 :: 4.1 added direct URL downloading via a single URL argument. | ||
* 2016 :: 4.2 added large dataURL support, a more semantic codebase, and hidden temp links | ||
* 20XX :: ???? Considering Zip, Tar, and other multi-file outputs, Blob.prototype.download option, and more, stay tuned folks. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25578
144
123