[RESOLVED] How to save a file after clicking on a link or button
I have file that is in a sql database. I have a web method that gets the file from the sql database as a binary. I call that web method using jquery ajax to get that binary data, which I need to save as a file in javascript. I've tried the following:
Code:
var contentType = '';
//binaryData is an array of bytes
function Save1(binaryData) {
var u16 = new Uint16Array(binaryData.length);
for(i=0; i<binaryData.length; i++){
u16[i]=binaryData[i];
}
var dataBlob= new Blob([u16], {type: "text/plain"});
var fileNameToSaveAs = "somefilename";
var downloadLink = document.createElement("a");
downloadLink.download = somefilename;
downloadLink.innerHTML = "Download File";
downloadLink.href = window.URL.createObjectURL(dataBlob);
document.body.appendChild(downloadLink);
}
when I left click the created link, nothing happens. I have to right click and select "save as" to save it. I'd like it to prompt me to save the file when I left click. What is wrong with the code or is there a better way of doing this in javascript? Thanks.
Edit:
It looks like the code above doesn't even work. When you right click to save, the file is no longer the same size.
Is there another way of saving a binary data in javascript?
Re: How to save a file after clicking on a link or button
Why not create a server side page that will write the file as output forcing the browser to download the file? My guess is that you are using ASP.Net for server side programming. If so, maybe something like this will help: http://www.c-sharpcorner.com/uploadf...le-in-Asp-Net/ ?
Google for more examples.
So, you will be providing the direct link to the download page in your link. Eg:
Code:
<a href="download.aspx?fileid=12345">Click to download the file..</a>
:wave:
Re: How to save a file after clicking on a link or button
It's because I have a modal window (by javascript), and I wanted to do the downloading without reloading the entire page or going to a different page. But I think your suggestion is the better approach. Question, when you click on that link button to "download.aspx", will that send the user to that page or will it just prompt the user the "Save as"? I'm assuming "fileid=12345" is the query string that will be used in the Page_Load method?
Re: How to save a file after clicking on a link or button
Quote:
Originally Posted by
benmartin101
It's because I have a modal window (by javascript), and I wanted to do the downloading without reloading the entire page or going to a different page. But I think your suggestion is the better approach. Question, when you click on that link button to "download.aspx", will that send the user to that page or will it just prompt the user the "Save as"? I'm assuming "fileid=12345" is the query string that will be used in the Page_Load method?
Okay. If your download page doesn't do anything except writing the file content(of the file that the user wishes to download) as output(with the headers), then I believe it won't display the blank downlaod page. Instead, it would just downloads the file. In my Chrome and Firefox, there's download folder already set in the browser config that will allow the file to download automatically to that location instead of popping up a window asking where to download.
And yes, the "fileid" was just given as an example so that your server side "download.aspx" could check this value of "fileid" and pull the file details from db or something. Or you could pass some other attr-value pair instead of that.
:wave:
Re: How to save a file after clicking on a link or button