2

I have file object

var File = new File(["aa"], "dek_iv");

can I download this one using JavaScript or jQuery

2
  • 3
    Possible duplicate of Download File Using Javascript/jQuery Commented May 29, 2018 at 5:58
  • 1
    I would suggest going another route, due to Browser compatibility. Commented May 29, 2018 at 6:40

2 Answers 2

6

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>

Sign up to request clarification or add additional context in comments.

7 Comments

No 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.
But 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);
I can't get you can you please elaborate once?
downloaded file is giving me html content not original content (aa) and for pdf type i am unable to open
You need to use some libraries if you want the HTML contents in a PDF file.
|
0

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>

1 Comment

Please enter a comment in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.