In this article I will explain with an example, how to download files from Folder (Directory) using jQuery in ASP.Net.
The files will be downloaded as BLOB using jQuery AJAX and XmlHttpRequest (XHR) and then will be sent for download in the Browser using jQuery.
 
 
Location of Files
The files are stored in a folder named Files inside the project directory.
Download Files from Folder (Directory) using jQuery in ASP.Net
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net DropDownList control.
The DropDownList has assigned with a JavaScript OnChange event handler.
Select File:
<asp:DropDownList runat="server" ID="ddlFiles" onchange="DownloadFile(this.value)">
    <asp:ListItem Text="" Value="" />
    <asp:ListItem Text="Sample.pdf" Value="Sample.pdf" />
    <asp:ListItem Text="Sample.docx" Value="Sample.docx" />
    <asp:ListItem Text="Sample.xlsx" Value="Sample.xlsx" />
    <asp:ListItem Text="Sample.txt" Value="Sample.txt" />
    <asp:ListItem Text="Sample.jpg" Value="Sample.jpg" />
</asp:DropDownList>
 
 
Downloading File from folder using jQuery
When an Item is selected (changed) in the DropDownList, the DownloadFile JavaScript function is called.
Inside the DownloadFile JavaScript function, the URL of the file is passed as parameter to the jQuery AJAX function.
Inside the jQuery AJAX function, using the XmlHttpRequest (XHR) call, the file is downloaded as Byte Array (Binary Data).
Note: The XmlHttpRequest (XHR) call is only supported in jQuery version 3.0 and higher.
 
Finally, the received Byte Array (Binary Data) is converted to BLOB object and the file is downloaded in Browser.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script type="text/javascript">
    function DownloadFile(fileName) {
        //Set the File URL.
        var url = "Files/" + fileName;
 
        $.ajax({
            url: url,
            cache: false,
            xhr: function () {
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 2) {
                        if (xhr.status == 200) {
                            xhr.responseType = "blob";
                        } else {
                            xhr.responseType = "text";
                        }
                    }
                };
                return xhr;
            },
            success: function (data) {
                //Convert the Byte Data to BLOB object.
                var blob = new Blob([data], { 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 = $("<a />");
                    a.attr("download", fileName);
                    a.attr("href", link);
                    $("body").append(a);
                    a[0].click();
                    $("body").remove(a);
                }
            }
        });
    };
</script>
 
 
Screenshot
Download Files from Folder (Directory) using jQuery in ASP.Net
 
 
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