Download files using the generic handler HttpHandler

Source: Internet
Author: User

In general, we can use HttpHandler to deal with some simple logic, such as verification code, download files and so on. To download the Word document as an example of how to download files in Hhtphandler, not limited to Word documents, if you download other files, you need to be aware of the "
Context. Response.ContentType = "Application/msword";
"Set to another appropriate format or common format" Application/octet-stream"To see the code
1             //file name2             Const stringFileName ="application form. doc";3             //Get file path4             stringPath = context. Server.MapPath ("./app_data/") +FileName;5             //defining output MIME Types6Context. Response.ContentType ="Application/msword";7             //download files as a file stream8             using(FileStream fs =NewFileStream (path, FileMode.Open))9             {Ten                 //Create a byte[] byte array with a length of file size to download One                 byte[] bytes =New byte[(int) fs. Length]; A                 //reads a file stream into a byte[] byte array -Fs. Read (Bytes,0, Bytes. Length); -                 //notifies the browser to download a file instead of opening it theContext. Response.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName, System.Text.Encoding.UTF8)); -                 //writes a byte[] byte array to the browser - context. Response.BinaryWrite (bytes); -}

Download files using the generic handler HttpHandler

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: 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.