ASP. NET uses generic handler for file download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.