Directly download the file instead of opening it in the browser

Source: Internet
Author: User

Files such as XML and PDF are usually opened in the browser when we provide them to customers for download, which is inconvenient.

 

The demo below can solve this problem, although this sectionCodeMany people will, but it is still sent here, as a backup.

 

 

  <% @ Import namespace = " System. xml "   %>

< Script Language = "C #" Runat = "Server" >
Void Page_load (Object sender, eventargs e ){
String path = Server. mappath ( " Somefileprohibited " );
System. Io. fileinfo File =   New System. Io. fileinfo (PATH );
Response. Clear ();
Response. addheader ( " Content-Disposition " , " Attachment; filename = "   + Httputility. urlencode (file. Name ));
Response. addheader ( " Content-Length " , File. length. tostring ());
Response. contenttype =   " Application/octet-stream " ;
Response. writefile (file. fullname );
Response. End ();
}
</ Script >  

 

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.