|
-
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.
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
|