Background Code HANDLER.ASHX
Copy Code code as follows:
<%@ WebHandler language= "C #" class= "Handler"%>
Using System;
Using System.Web;
public class Handler:ihttphandler {
public void ProcessRequest (HttpContext context) {
string filename = "Web.config";//client saved file name
String FilePath = context. Server.MapPath ("web.config");//path
Downloading files as streams of characters
System.IO.FileStream fs = new System.IO.FileStream (FilePath, System.IO.FileMode.Open);
byte[] bytes = new byte[(int) fs. Length];
Fs. Read (bytes, 0, bytes. Length);
Fs. Close ();
Context. Response.ContentType = "Application/octet-stream";
Notifies the browser to download the file instead of opening it
Context. Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (FileName, System.Text.Encoding.UTF8));
Context. Response.BinaryWrite (bytes);
Context. Response.Flush ();
Context. Response.End ();
}
public bool IsReusable {
get {
return false;
}
}
}
Front-End Code:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script src= "Jquery-1.7.2.min.js" type= "Text/javascript" ></script>
<title></title>
<script>
function Download_file (URL)
{
if (typeof (download_file.iframe) = = "undefined")
{
var iframe = document.createelement ("iframe");
Download_file.iframe = iframe;
Document.body.appendChild (Download_file.iframe);
}
alert (download_file.iframe);
download_file.iframe.src = URL;
Download_file.iframe.style.display = "None";
}
</script>
<body>
<a href= "javascript:void (0);" onclick= "Download_file (' handler.ashx ')" >aaaaa</a>
<a href= "javascript:void (0);" onclick= "Download_file (' handler.ashx ')" >bbbbb</a>
<a href= "javascript:void (0);" onclick= "Download_file (' handler.ashx ')" >ccccc</a>
</body>