Many tutorials can be found on the Internet to download JS files. However, this article introduces you to the implementation of Brushless newest download files. It seems cool, right? Background code Handler. ashx
The 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:
The Code is as follows: