Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
In the application, we may encounter this situation. We need to generate a temporary data file for the user to download, but each download must be judged, that is, the user cannot get the downloaded url to download files continuously. The following is the implementation method. The file is saved in csv format to facilitate data import and export. The basic principle is to use a stream to write a file and then use Response. WriteFile to send the stream to the client.
The structure of the table is the same as this article: http://blog.111cn.net/lovecherry/archive/2005/02/25/301441.aspx
SqlConnection conn = new SqlConnection (System. Configuration. ConfigurationSettings. Deleettings ["conn"]);
SqlDataAdapter da = new SqlDataAdapter ("select * from tb1", conn );
DataSet ds = new DataSet ();
Da. Fill (ds, "table1 ");
DataTable dt = ds. Tables ["table1"];
String name = System. configuration. configurationSettings. appSettings ["downloadurl"]. toString () + DateTime. today. toString ("yyyyMMdd") + new Random (DateTime. now. millisecond ). next (1, 10000 ). toString () + ". csv "; // save it to the web. the path specified by downloadurl in config. The file format is the current date + 4-digit random number.
FileStream fs = new FileStream (name, FileMode. Create, FileAccess. Write );
StreamWriter sw = new StreamWriter (fs, System. Text. Encoding. GetEncoding ("gb2312 "));
Sw. WriteLine ("automatic number, name, age ");
Foreach (DataRow dr in dt. Rows)
Sw. WriteLine (dr ["ID"] + "," + dr ["vName"] + "," + dr ["iAge"]);
Sw. Close ();
Response. AddHeader ("Content-Disposition", "attachment; filename =" + Server. UrlEncode (name ));
Response. ContentType = "application/ms-excel"; // specify that the returned stream cannot be read by the client and must be downloaded.
Response. WriteFile (name); // send the file stream to the client
Response. End ();
Before this code, you can place some judgments to determine whether the user can download the file. The generated file can store the name in the database. Next time, you can download the file directly instead of writing the file repeatedly.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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.