General methods for downloading Asp.net files

Source: Internet
Author: User

In asp.net, you can simply write a file stream in response, which can be read by the browser by mistake. For example, an image or a .htm will be displayed directly when it is opened by the browser. However, the pop-up save panel prompts you to save.
The following steps are required. CopyCodeThe Code is as follows: Response. Clear ();
Response. addheader ("content-disposition", "attachment; filename =" + clientname );
Response. contenttype = "application/octet-stream ";
Response. writefile (filename); // response. transmitfile (filename );

The purpose is:
Clear the existing content in the response (because you may be using it in Aspx. CS, or some content in httpresponse without your knowledge)
Modify httpheader,
Modify MIME type
Write File stream
The last line can be writefile or transmitfile, but it is slightly different. It is more efficient to use transmitfile because it "writes the specified file directly to the HTTP Response output stream, instead of caching the file in the memory ".
Here, mime and httpheader are suitable for downloading all types of files.

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.