Skip to content

Instantly share code, notes, and snippets.

@phamquocbuu
Created November 6, 2013 03:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phamquocbuu/7330466 to your computer and use it in GitHub Desktop.
Save phamquocbuu/7330466 to your computer and use it in GitHub Desktop.
var a = document.createElement('a');
a.download = filename; // Set the file name.
a.style.display = 'none';
document.body.appendChild(a);
a.click();
delete a;
@techsoumya
Copy link

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();
}

@code-atom
Copy link

@voidprady
Copy link

Hi,
how can i get the filename that was used, when the file was uploaded onto the server.

@Achilles718611
Copy link

It does not work.

@roeiAnyVision
Copy link

roeiAnyVision commented Mar 4, 2018

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