Background code Handler. ashx
<% @ WebHandler Language = "C #" Class = "Handler" %> using System; using System. web; public class Handler: IHttpHandler {public void ProcessRequest (HttpContext context) {string fileName = "web. config "; // the file name saved by the client string filePath = context. server. mapPath ("web. config "); // path // download the file System in the form of a response stream. 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 the 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:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">