How to download a file in JavaScript or jQuery
14,611
Here is an Example how you can download your file Object.
But this will return nothing since the file size is too small for the system to read.
function downloadFile() {
var file = new File(["aa"], "dek_iv.txt");
var link = document.createElement("a");
link.download =file.name;
link.href = file;
link.click();
}
<button onclick="downloadFile()">Download File</button>
Related videos on Youtube
Author by
ravindra reddy
Updated on June 04, 2022Comments
-
ravindra reddy almost 2 years
I have file object
var File = new File(["aa"], "dek_iv");
can I download this one using JavaScript or jQuery
-
ozil almost 6 yearsPossible duplicate of Download File Using Javascript/jQuery
-
StackSlave almost 6 yearsI would suggest going another route, due to Browser compatibility.
-
-
Harish Soni almost 6 yearsNo problem. If you got the solution then if you can accept it as answer and give an upvote so that others can easily get the answer if they have the same issue.
-
ravindra reddy almost 6 yearsBut the file content is coming as html i want to see aa in the file i changed below line now its coming as text link.href = URL.createObjectURL(file);
-
Harish Soni almost 6 yearsI can't get you can you please elaborate once?
-
ravindra reddy almost 6 yearsdownloaded file is giving me html content not original content (aa) and for pdf type i am unable to open
-
Harish Soni almost 6 yearsYou need to use some libraries if you want the HTML contents in a PDF file.
-
Harish Soni almost 6 years
-
Jarry over 5 years"But this will return nothing since the file size is too small for the system to read." can you expand on this? i can't find any reference to a minimum file size
-
Özgür Can Karagöz almost 4 yearsPlease enter a comment in your answer.