Created
April 22, 2020 09:02
-
-
Save bartwttewaall/1f8d00c6df82696f16665414c3db746a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setupDownload() { | |
// quick test to check if download is available on anchors (fake Modernizr implementation) | |
var anchor = document.createElement("A"); | |
var Modernizr = { adownload: "download" in anchor }; | |
delete anchor; | |
if (Modernizr.adownload) { | |
var downloads = document.querySelectorAll("a[download]"); | |
for (var i = 0; i < downloads.length; i++) { | |
downloads[i].addEventListener("click", function(event) { | |
event.preventDefault(); | |
var url = event.currentTarget.href; | |
var filename = event.currentTarget.download; | |
var mimetype = event.currentTarget.dataset.mimetype; | |
// download(url, filename, mimetype); | |
var x = new XMLHttpRequest(); | |
x.open("GET", url, true); | |
x.responseType = "blob"; | |
x.onload = function(e) { | |
download(x.response, filename, mimetype); | |
}; | |
x.send(); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment