ASP. NET solves the problem of downloading files from the server. asp.net downloads files
Suppose there is a Download folder under the root directory of the server, which stores some files that are provided to the reference program for Download.
Public void DownloadFile (string path, string name ){
Try {
System. IO. FileInfo file = new System. IO. FileInfo (path );
Response. Clear ();
Response. Charset = "GB2312 ";
Response. ContentEncoding = System. Text. Encoding. UTF8;
// Add the header information and specify the default file name for the "download/Save as" dialog box
Response. AddHeader ("Content-Disposition", "attachment; filename =" + Server. UrlEncode (name ));
// Add header information and specify the file size so that the browser can display the download progress.
Response. AddHeader ("Content-Length", file. Length. ToString ());
// Specify a stream that cannot be read by the client and must be downloaded.
Response. ContentType = "application/ms-excel ";
// Send the file stream to the client
Response. WriteFile (file. FullName );
// Stop page execution
// Response. End ();
HttpContext. Current. ApplicationInstance. CompleteRequest ();
}
Catch (Exception ex ){
Response. Write ("<script> alert ('System error: // n" + ex. Message + "! // N contact the Administrator as soon as possible. ') </script> ");
}
}
This function is a program for downloading the function. path indicates the absolute path of the file (including the file name), and name indicates the file name. This program can run. if you set HttpContext. current. applicationInstance. completeRequest (); Replace with Response. end (); an error occurs: exception: the expression value cannot be calculated because the code has been optimized or the local framework is located on the call stack. however, this error does not affect the running of the program. Although try can catch this exception (I don't know why)
I found some reasons for this problem on the Internet: If the Response. End, Response. Redirect or Server. Transfer method is used, a ThreadAbortException exception occurs. You can use the try-catch statement to catch this exception. The Response. End method terminates the page execution and switches this execution to the Application_EndRequest event in the event pipeline of the application. Do not execute the code line after Response. End. This problem occurs in the Response. Redirect and Server. Transfer methods, because both methods call Response. End internally.
The following solutions are provided:
To solve this problem, use one of the following methods:
For Response. End, call HttpContext. Current. ApplicationInstance. CompleteRequest () instead of Response. End to skip the code execution of the Application_EndRequest event.
For Response. Redirect, use the overload Response. Redirect (String url, bool endResponse). This overload will pass false to the endResponse parameter to cancel internal calls to Response. End. For example:
Response. Redirect ("nextpage. aspx", false );
Catch (System. Threading. ThreadAbortException e ){
Throw;
}
Next, you can use other functions or events to call this function to download files on the server.
Protected void btnOutput_Click (object sender, EventArgs e ){
Try {
String strPath = Server. MapPath ("/") + "Download // student Basic Information Template .xls ";
DownloadFile (strPath, "Student Basic Information Template .xls ");
}
Catch (Exception exp ){
Response. Write ("<script> alert ('System error: // n" + exp. Message + "! // N contact the Administrator as soon as possible. ') </script> ");
}
}
From this event, we can see that the first parameter of the DownloadFile function is the absolute path of the file. Otherwise, the program will report an error.