Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download file using JavaScript only

I have an only JavaScript page and .asmx page. I want to download file using only JavaScript how can I download the file. I want to download a particular resume.

image

I am getting resume here,

var res = data[i].resume;
like image 709
amit Avatar asked Nov 12 '15 04:11

amit


2 Answers

You may use different third-party libraries:

jQuery.fileDownload

It takes URL as an input and downloads a file while shows a loading dialog.

Github: https://github.com/johnculviner/jquery.fileDownload
Demo: http://jqueryfiledownload.apphb.com/

Usage:

$.fileDownload(requestUrl, {
    preparingMessageHtml: "Downloading...",
    failMessageHtml: "Error, please try again."
});

FileSaver.js

It takes Blob object as an input and downloads it. Blob can be acquired using XMLHttpRequest.

Github: https://github.com/eligrey/FileSaver.js/
Demo: http://eligrey.com/demos/FileSaver.js/

Usage:

var xhr = new XMLHttpRequest();
xhr.open("GET", requestUrl);
xhr.responseType = "blob";

xhr.onload = function () {
    saveAs(this.response, 'filename.txt'); // saveAs is a part of FileSaver.js
};
xhr.send();

It may also be used to save canvas-based images, dynamically generated text and any other Blobs.

Or write it yourself

function saveData(blob, fileName) // does the same as FileSaver.js
{
    var a = document.createElement("a");
    document.body.appendChild(a);
    a.style = "display: none";

    var url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = fileName;
    a.click();
    window.URL.revokeObjectURL(url);
}

Now, if it is a text file, you can simply download it, create a blob, and save it:

$.ajax({ 
    url: requestUrl,
    processData: false,
    dataType: 'text'
}).done(function(data) {
    var blob = new Blob([data], { type: "text/plain; encoding=utf8" });
    saveData(blob, 'filename.txt');    
});

Or you can use XMLHttpRequest which works great for any types of files, including binary:

var xhr = new XMLHttpRequest();
xhr.open("GET", requestUrl);
xhr.responseType = "blob";

xhr.onload = function () {
    saveData(this.response, 'filename'); // saveAs is now your function
};
xhr.send();

Here is the working demo. Note that this fiddle downloads a file right after opening it. The file is just a random source file from GitHub.

like image 194
Yeldar Kurmangaliyev Avatar answered Oct 18 '22 16:10

Yeldar Kurmangaliyev


Actually, There is a javascript library called FileSaver.js, FileSaver.js saving file on the client-side. it can help you achieve this.

here: https://github.com/eligrey/FileSaver.js

Usage:

<script src="http://cdn.jsdelivr.net/g/filesaver.js"></script>
<script>
  function SaveAsFile(t,f,m) {
    try {
      var b = new Blob([t],{type:m});
      saveAs(b, f);
    } catch (e) {
      window.open("data:"+m+"," + encodeURIComponent(t), '_blank','');
    }
  }

SaveAsFile("text","filename.txt","text/plain;charset=utf-8");

</script>
like image 2
Kexin Li Avatar answered Oct 18 '22 14:10

Kexin Li