Download files from folder (directory) using jQuery in ASP.Net


The files are stored in a folder called records inside the project directory.


The following HTML markup consists of an ASP.Net DropDownList control.


The DropDownList has been assigned with a JavaScript At change event handler.


Select File:


<asp:The drop down list speak=”server” ID=”ddlFiles” in the change=”DownloadFile(this.value)”>


<asp:list item Text=”” Worth=”” />


<asp:list item Text=”Sample.pdf” Worth=”Example.pdf” />


<asp:list item Text=”Example.docx” Worth=”Example.docx” />


<asp:list item Text=”Sample.xlsx” Worth=”Sample.xlsx” />


<asp:list item Text=”Sample.txt” Worth=”Example.txt” />


<asp:list item Text=”Sample.jpg” Worth=”Sample.jpg” />


asp:The drop down list>


When an item in the DropDownList is selected (changed), the Download file The JavaScript function is called.


Finally, the received byte array (binary data) is converted to a BLOB object and the file is downloaded to the browser.


<script write=”text/javascript” source=”https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js”>script>


<script write=”text/javascript”>


function Download file (filename) {


//Set the URL of the file.


I was url = “Records/” + filename;


$.ajax({


URL: URL,


cache: fake,


xr: function () {


I was x hour = new XMLHttpRequest();


xhr.onreadystatechange = function () {


Yes (xhr.readyState == 2) {


Yes (xhr.status == 200) {


xhr.responseType = “drop”;


} plus {


xhr.responseType = “text”;


}


}


};


return xr;


},


success: function (data) {


//Convert the byte data to a BLOB object.


I was drop = new Drop([data]{ write: “application/byte stream” });

See also  PlayStation gamers could receive up to £562 each from £5bn lawsuit


//Check the browser type and download the file.


I was isIE = fake || !!document.documentMode;


Yes (esIE) {


window.navigator.msSaveBlob(blob, filename);


} plus {


I was url = window.URL || window.webkitURL;


link = url.createObjectURL(blob);


I was a = $();


a.attr(“to download”File name);


a.attr(“href”Link);


$“Body”).add(to);


a[0].click();


$“Body”).remove(a);


}


}


});


};


script>

Leave a Comment