Created
November 6, 2013 03:33
-
-
Save phamquocbuu/7330466 to your computer and use it in GitHub Desktop.
JS Save a file from a URL with JavaScript http://ausdemmaschinenraum.wordpress.com/2012/12/06/how-to-save-a-file-from-a-url-with-javascript/
This file contains 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
var a = document.createElement('a'); | |
a.download = filename; // Set the file name. | |
a.style.display = 'none'; | |
document.body.appendChild(a); | |
a.click(); | |
delete a; |
this http://www.muazkhan.com/2012/10/save-files-on-disk-using-javascript-or.html
This may help you!
Hi,
how can i get the filename that was used, when the file was uploaded onto the server.
It does not work.
you are missing a.href = "URL to download"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have used similar piece of code for my dummy project.Its working fine in chrome but not in IE 11.Please guide me how can i able to download the file in IE.Please find the below code snippet what i have used.
function saveFile(url) {
// Get file name from url.
var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
a.download = filename; // Set the file name.
a.style.display = 'none';
document.body.appendChild(a);
a.click();
delete a;
};
xhr.open('GET', url);
xhr.send();
}