ravindra reddy
ravindra reddy

Reputation: 110

How to download a file in JavaScript or jQuery

I have file object

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

can I download this one using JavaScript or jQuery

Upvotes: 2

Views: 15091

Answers (2)

Sumon Hossain
Sumon Hossain

Reputation: 1

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>

Upvotes: 0

Harish Soni
Harish Soni

Reputation: 1896

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>

Upvotes: 6

Related Questions