First there is an HTML page, the page has a link, click the link pop-up file download/save (similar to Thunder download link)
<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <title>File download</title> <MetaCharSet= "Utf-8" /></Head><Body> <!--this way not, 1: If the access is similar to the text browser can process the file, it is the way the browser opens the display, not the file download; 2: If you are accessing files in the App_Data folder, the. NET mechanism does not allow access to the App_Data folder resource. Therefore, the request filtering module is configured to reject the path in the URL that contains the Hiddensegment section. " - <ahref= "App_data/readme.txt">Download Readme.txt File</a> <BR/> <ahref= "Downloadfilehandler.ashx">Download Readme.txt File</a></Body></HTML>
View Code
The code for the generic handler is as follows
usingSystem.IO;usingsystem.web;namespacezhong.web{/// <summary> ///Summary description of Downloadfilehandler/// </summary> Public classDownloadfilehandler:ihttphandler { Public voidProcessRequest (HttpContext context) {stringFilePath = context. Server.MapPath ("~/app_data/readme.txt"); FileStream FS=NewFileStream (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 (); //solutions for large file downloads//context. Response.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 BOOLisreusable {Get { return false; } } }}
View Code
Click on the first link to access it as shown below:
Click the second link to access and download the file:
Since I have already tested it before, this download is named Readme (1). txt
ASP. NET uses generic handler for file download