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 ();
}