In this article I will explain with an example, how to save BLOB (Binary Data) as file using JavaScript.
The BLOB (Binary Data) sent from Server will be received using XmlHttpRequest AJAX call and then will be sent for download in the Browser using JavaScript.
 
 
Location of Files
The PDF file are stored in a folder named Files inside the project directory.
Save BLOB as File using JavaScript
 
Note: You can also set URL of the PDF which can belong to either same server or any other server location.
 
 
Saving BLOB as File using JavaScript
When the Download Button is clicked, the DownloadFile JavaScript function is called.
Inside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the GET call of the JavaScript XmlHttpRequest call.
Then inside the onload event handler, the received Byte Array (Binary Data) is converted to BLOB object and the File is downloaded in Browser.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <input type="button" value="Download PDF File" onclick="DownloadFile('Sample.pdf')" />
    <script type="text/javascript">
        function DownloadFile(fileName) {
            //Set the File URL.
            var url = "Files/" + fileName;
 
            //Create XMLHTTP Request.
            var req = new XMLHttpRequest();
            req.open("GET", url, true);
            req.responseType = "blob";
            req.onload = function () {
                //Convert the Byte Data to BLOB object.
                var blob = new Blob([req.response], { type: "application/octetstream" });
 
                //Check the Browser type and download the File.
                var isIE = false || !!document.documentMode;
                if (isIE) {
                    window.navigator.msSaveBlob(blob, fileName);
                } else {
                    var url = window.URL || window.webkitURL;
                    link = url.createObjectURL(blob);
                    var a = document.createElement("a");
                    a.setAttribute("download", fileName);
                    a.setAttribute("href", link);
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                }
            };
            req.send();
        };
    </script>
</body>
</html>
 
 
Screenshot
Save BLOB as File using JavaScript
 
 
Browser Compatibility
The above code has been tested in the following browsers only in versions that support HTML5.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Demo
 
 
Downloads