Under the ASP.net File

Source: Internet
Author: User
When the system file is directly downloaded, the program will be called to open the file, or you can download the file to the user after verification. This method can be used to achieve this.

Private void FileDown (string strPath)
{
System. IO. FileInfo file = new System. IO. FileInfo (strPath );
If (file. Exists)
{
Response. Clear ();

Response. AddHeader ("Content-Disposition", "attachment; filename =" +
HttpUtility. UrlEncode (file. FullName, System. Text. Encoding. UTF8 ));
Response. AddHeader ("Content-Length", file. Length. ToString ());
Response. ContentType = "application/octet-stream ";
Response. Filter. Close ();
Response. WriteFile (file. FullName );
Response. End ();
}
Else
{

ClientScript. RegisterStartupScript (GetType (), "", "<script
Language = 'javascript '> alert ('file does not exist! '); </Script> ");
}
}

By the way, press the GridView to convert it into an Excel file for the user to download the code.

Private void DownExcel ()
{
Response. ClearContent ();
Response. AddHeader ("content-disposition", "attachment; filename=print.xls ");
Response. ContentType = "application/ms-excel ";
StringWriter sw = new StringWriter ();
HtmlTextWriter htw = new HtmlTextWriter (sw );
GridView1.RenderControl (htw );
Response. Write (sw. ToString ());
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.