ASP. NET direct File Download

Source: Internet
Author: User

1. The server outputs the corresponding HTTP Response Headers information through 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">
HTTP-equiv indicates the headers name, and content indicates the value of this headers.

2. First, the MIME type of the file to be output:
Page. response. addheader ("Content-Type", "MIME type ");

3. Second, output the Open Location and file name of the downloaded file:
Page. response. addheader ("content-disposition", "attachment; filename =" + filename );
The HTTP Response Header of content-Disposition allows you to specify the information displayed in the document. With this header, you can specify the document to be opened separately (instead of in the browser) and display it according to user operations. If you want to save a document, you can also recommend a file name for this document. The recommended name will appear in the "file name" column of the Save As dialog box.
Attachment-the attachment is sent to the client, and the client opens the file separately.
Inline-indicates that the file will be opened in the browser.
Filename-indicates the file name sent to the client.

4. file data to be sent to the client:
No matter what type of file, convert it to an array of the byte type, and then output the byte array 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: 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.