Asp.net implements file download using a general handler, and asp.net downloads files
First, there is an html page with a link. Click the link to bring up the file download/Save (similar to the thunder download link)
<! DOCTYPE html> View Code
The general processing code is as follows:
Using System. IO; using System. web; namespace Zhong. summary of Web {/// <summary> /// DownloadFileHandler /// </summary> public class DownloadFileHandler: IHttpHandler {public void ProcessRequest (HttpContext context) {string filePath = context. server. mapPath ("~ /App_Data/readme.txt "); FileStream fs = new FileStream (filePath, FileMode. open); byte [] bytes = new byte [fs. length]; fs. read (bytes, 0, bytes. length); fs. dispose (); context. response. contentType = "application/octet-stream"; context. response. addHeader ("Content-Disposition", "attachment; filename=readme.txt"); context. response. binaryWrite (bytes); context. response. flush (); // large file download solution // context. respon Se. contentType = "application/x-zip-compressed"; // context. response. addHeader ("Content-Disposition", "attachment?filename=z.zip"); // string filename = Server. mapPath ("~ /App_Data/move.zip "); // context. Response. TransmitFile (filename);} public bool IsReusable {get {return false ;}}}}
View Code
Click the first link to access the table, as shown in the following figure:
Click the second link to access and download the file:
Since I have tested it, the name of this download is readme(1).txt