2

I have file object

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

can I download this one using JavaScript or jQuery

RBT
  • 24,161
  • 21
  • 159
  • 240
ravindra reddy
  • 110
  • 1
  • 1
  • 9
  • 3
    Possible duplicate of [Download File Using Javascript/jQuery](https://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) – ozil May 29 '18 at 05:58
  • 1
    I would suggest going another route, due to Browser compatibility. – StackSlave May 29 '18 at 06:40

2 Answers2

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>
Harish Soni
  • 1,796
  • 12
  • 27
  • 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. – Harish Soni May 29 '18 at 06: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); – ravindra reddy May 29 '18 at 06:58
  • I can't get you can you please elaborate once? – Harish Soni May 29 '18 at 07:03
  • downloaded file is giving me html content not original content (aa) and for pdf type i am unable to open – ravindra reddy May 29 '18 at 07:05
  • You need to use some libraries if you want the HTML contents in a PDF file. – Harish Soni May 29 '18 at 07:06
  • https://stackoverflow.com/questions/47989089/create-pdf-from-reactjs/47989135#47989135 – Harish Soni May 29 '18 at 07:06
  • "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 – Jarry Jan 15 '19 at 16:50
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>