2

I have file object

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

can I download this one using JavaScript or jQuery

2

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>

7
  • 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. Commented May 29, 2018 at 6:11
  • 1
    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); Commented May 29, 2018 at 6:58
  • I can't get you can you please elaborate once? Commented May 29, 2018 at 7:03
  • downloaded file is giving me html content not original content (aa) and for pdf type i am unable to open Commented May 29, 2018 at 7:05
  • You need to use some libraries if you want the HTML contents in a PDF file. Commented May 29, 2018 at 7:06
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.