Asp. NET Direct download file

Source: Internet
Author: User

A. The server sends the corresponding HTTP Response headers information via Response, and the data of the file to be downloaded to send the file to the client, the HTTP Response headers is shown in the following form in the HTML file:
<meta http-equiv= "Content-type" content= "text/htm" >
The HTTP-EQUIV representation is the name of the headers, and the content represents the value of the headers

Two. First, to output the MIME type of the file:
Page.Response.AddHeader ("Content-type", "MIME type");

Three. Second, to output the downloaded file's open location and file name:
Page.Response.AddHeader ("Content-disposition", "attachment;filename=" + filename);
The Content-disposition HTTP response header allows you to specify the information that the document represents. With this header, you can specify that the document be opened individually (not in a browser), and can be displayed according to the user's actions. If the user wants to save the document, you can also suggest a file name for the document. This suggestion name appears in the file Name column of the Save as dialog box.
Attachment―― is sent to the client as an attachment, and the client opens the file separately.
Inline―― indicates that the file will be opened in the browser.
Filename―― represents the file name sent to the client file.

Four. Prepare the file data to be sent to the client:
Regardless of the type of file to be converted to a byte-type array first, the byte array is then output to the client using the Response.BinaryWrite method.

String path = "G://download//down.txt";
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 ();

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