How can I put data in a folder inaccessible to the web and download it to users?

Source: Internet
Author: User
Tags datetime
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:
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.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.