This article mainly introduces the ASP. NET implementation from the server download file problem processing, has a good reference value, followed by a small series to see it
Suppose you have a folder named download under the root of the server, and this folder holds some files that are available for download to the reference program
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 header information for the file download/Save As dialog box to specify the default file name Response.AddHeader ("content-disposition", "attachment; Filename= "+ server.urlencode (name)); Add header information, specify the file size, so that the browser can display the download progress Response.AddHeader ("Content-length", file. Length.tostring ()); The specified return is a stream that cannot be read by the client and must be downloaded Response.ContentType = "Application/ms-excel"; Sends the file stream to the client response.writefile (file. FullName); Stop the execution of the page //response.end (); HttpContext.Current.ApplicationInstance.CompleteRequest (); } catch (Exception ex) { Response.Write ("<script>alert (" the system has the following error://n "+ ex.) Message + "!//n please contact the administrator as soon as possible." </script> "); } }
This function is the download function of the group program, where path is the absolute path of the file (including the file name), name is the filename, This program is capable of running. If HttpContext.Current.ApplicationInstance.CompleteRequest () is replaced with Response.End (); There will be an error: Exception: The value of an expression cannot be evaluated because the code has been optimized or the native frame is above the call stack. But this error does not affect the operation of the program, although try to catch the exception (do not know why)
Found some reason for this problem on the Internet: If you use the Response.End, Response.Redirect, or Server.Transfer methods, a ThreadAbortException exception will occur. You can use the Try-catch statement to catch this exception. The Response.End method terminates the execution of the page and switches this execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed. This problem occurs in the Response.Redirect and Server.Transfer methods, because both methods call Response.End internally.
The workarounds provided are:
To resolve this issue, use one of the following methods:
For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest () method instead of Response.End to skip Application_ The code execution for the EndRequest event.
for Response.Redirect, use the overloaded Response.Redirect (String URL, bool endresponse), which passes false to the Endresponse parameter to cancel the Response . The internal invocation of End. For example:
Response.Redirect ("Nextpage.aspx", false); catch (System.Threading.ThreadAbortException e) { throw;} This function can then be called by other functions or events to download the file 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 (" the system has the following error://n "+ exp. Message + "!//n please contact the administrator as soon as possible." </script> "); } }
From this event, you can see that the first parameter of the DownloadFile function is the absolute path of the file or the program will error.