C # how to download a hidden file

Source: Internet
Author: User

The download path is hidden by passing the ID in the database, and then reading the path in the database through the passed ID on the other page.
The implementation method in c # (only the. ASPX. CS code is required)
String sel_ SQL = "select FileVisualPath from TabMailAttachFiles where FileID =" Request. QueryString ["destFileName"]. ToString ();

// String destFileName = Request. QueryString ["destFileName"]! = Null? Request. QueryString ["destFileName"]: "";
String destFileName = ROA. Components. fune_performance.execad (sel_ SQL). Tables [0]. Rows [0] [0]. ToString ();
DestFileName = Server. MapPath (".") destFileName;
DestFileName = Server. UrlDecode (destFileName );
If (File. Exists (destFileName ))
{
FileInfo fi = new FileInfo (destFileName );
Response. Clear ();
Response. ClearHeaders ();
Response. Buffer = false;

// Response. AppendHeader ("Content-Disposition", "attachment; filename =" HttpUtility. UrlEncode (Path. GetFileName (destFileName), System. Text. Encoding. Default ));
Response. AppendHeader ("Content-Disposition", "attachment; filename =" HttpUtility. UrlEncode (Path. GetFileName (destFileName), System. Text. Encoding. UTF8 ));
Response. AppendHeader ("Content-Length", fi. Length. ToString ());
Response. ContentType = "application/octet-stream ";
Response. WriteFile (destFileName );
Response. Flush ();
Response. End ();
}
Else
{
Response. Write ("<script langauge = javascript> alert (the file does not exist !); History. go (-1); </script> ");
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.