ASP. NET implements file Download function

Source: Internet
Author: User
This article mainly introduces the ASP. NET use General processing program to achieve file download function, very good, with reference value, the need for friends can refer to the next

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>

The code for the generic handler is as follows


Using system.io;using system.web;namespace zhong.web{//<summary>//Downloadfilehandler Summary description///</summary > public class Downloadfilehandler:ihttphandler {public void ProcessRequest (HttpContext context) {string Filepa th = 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 (); Solution for large file download//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 bool IsReusable {get {return false; }  } }}

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

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.