Asp.net common download Methods

Source: Internet
Author: User

1. transmitfile to solve the problem that the aspnet_wp.exe process cannot be successfully downloaded when response. binarywrite downloads a file larger than MB.

String filename = "Planning Information Data Warehouse field description .doc"; // file name saved by the client
Response. contenttype = "application/X-zip-compressed ";
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. Text. encoding. utf8 ));
String filename = server. mappath ("download/Planning Information Data Warehouse field description .doc ");
Response. transmitfile (filename );

2. Download in the form of a stream

String filename = "Planning Information Data Warehouse field description .doc"; // file name saved by the client
String filepath = server. mappath ("download/Planning Information Data Warehouse field description .doc"); // path
// Download an object in the form of a streaming
Filestream FS = new filestream (filepath, filemode. Open );
Byte [] bytes = new byte [(INT) fs. Length];
FS. Read (bytes, 0, bytes. Length); // read to the specified array
FS. Close ();
Response. contenttype = "application/octet-stream ";
// Notify the browser to download the file instead of opening it
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. Text. encoding. utf8 ));
Response. binarywrite (bytes );
Response. Flush ();
Response. End ();

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.