|
-
Jun 18th, 2015, 07:25 PM
#1
Thread Starter
Frenzied Member
[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?
Last edited by benmartin101; Jun 18th, 2015 at 08:22 PM.
-
Jun 19th, 2015, 08:41 AM
#2
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>
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Jun 19th, 2015, 05:29 PM
#3
Thread Starter
Frenzied Member
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?
-
Jun 20th, 2015, 05:59 AM
#4
Re: How to save a file after clicking on a link or button
 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.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Jun 21st, 2015, 06:44 PM
#5
Re: How to save a file after clicking on a link or button
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|