Asp.net dynamically generates TXT files and provides users with download code

Source: Internet
Author: User

The following function is to use Asp.net to automatically generate a TXT text file based on the data content and provide users with download. This method does not save the file on the server and is directly provided to users for download, I searched the internet and used response. binarywrite (), used a few times, found that there is a problem in IE, look at the pageSource codeThe data is generated. In Firefox, a box is displayed to download the data, but not txt, but Aspx. The content in the box is the data obtained from the database.

In fact, you can simply use write () in ASP. NET,CodeAs follows (. aspx. CS file ):

Response. Clear ();
Response. Buffer = false;
Response. contenttype = "application/octet-stream ";
Response. appendheader ("content-disposition", "attachment; filename =" + filename + ". txt ;");
Response. Write ("1234 | ABCDE/R/N ");

// Read the database, loop
For (I = 0; I <100; I ++)
{
Response. Write ("1234 | ABCDE/R/N ");
}
Response. Flush ();
Response. End ();

Both IE and Firefox

This article from: It Knowledge Network (http://www.itwis.com) detailed source reference: http://www.itwis.com/html/net/aspnet/20081016/2599.html

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.