Asp.net implements the file download function using a general handler.

Source: Internet
Author: User

Asp.net implements the file download function using a general handler.

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> 

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 ;}}}}

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

Related Article

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.