asp.net file Download General method _ Practical Tips

Source: Internet
Author: User
In ASP.net, when you write a file stream simply in response, you may be misread by the browser, such as a picture or. htm, which will appear directly when the browser is opened. But the demand is, pop-up saved panel prompts the user to save.
So, here are a few steps that are essential.
Copy Code code as follows:

Response.Clear ();
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ clientname);
Response.ContentType = "Application/octet-stream";
Response.WriteFile (filename);//response.transmitfile (filename);

The objectives are:
Empty the response already content (because you are most likely to be used in aspx.cs, or if you do not know, HttpResponse already have some content)
Modify Httpheader,
modifying MIME types
Write to file stream
The last line, using either WriteFile or TransmitFile, can be, but slightly different, better at using transmitfile efficiency, because it "writes the specified file directly to the HTTP response output stream without buffering the file in memory."
And here's mime and httpheader for all types of file downloads.

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.