Background code Handler. ashx
Copy codeThe Code is 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"; // file name saved by the client
String filePath = context. Server. MapPath ("web. config"); // path
// Download an object in the form of a streaming
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 ";
// Notify 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 codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<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>
</Head>
<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>
</Html>