ASP. NET implementation of downloading files from the server

  • 2021-09-12 00:56:05
  • OfStack

Suppose you have a folder named Download under the root directory of the server, which holds a few files for the referencing program to 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 header information for " 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, and let the browser display the download progress 
  Response.AddHeader("Content-Length", file.Length.ToString());
  //  Specifies that the returned is 1 Streams that cannot be read by the client must be downloaded 
  Response.ContentType = "application/ms-excel";
  //  Send 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 experienced the following error ://n" + ex.Message + "!//n Please contact the administrator as soon as possible .')</script>");
 }
 }

This function is the download function group program, where path is the absolute path of the file (including the file name), name is the file name, this program is able to run. Among them, if HttpContext. Current. ApplicationInstance. CompleteRequest (); Replace with Response. End (); One error occurs: Exception: The value of the expression cannot be evaluated because the code has been optimized or the native framework is on the call stack. But this error does not affect the program, although try can catch this exception (I don't know why)

I found a few reasons for this problem on the Internet: If you use Response. End, Response. Redirect or Server. Transfer methods, you will have an ThreadAbortException exception. You can catch this exception using the try-catch statement. The Response. End method terminates the execution of the page and switches this execution to the Application_EndRequest event in the event pipeline of the application. The lines following Response. End are not executed. This problem occurs in the Response. Redirect and Server. Transfer methods, because both methods call Response. End internally.

The solutions provided are:

To solve this problem, use one of the following methods:

For Response. End, call the HttpContext. Current. ApplicationInstance. CompleteRequest () method instead of Response. End to skip code execution of the Application_EndRequest event.

For Response. Redirect, use the overload Response. Redirect (String url, bool endResponse), which passes false to the endResponse parameter to cancel the internal call to Response. End. For example:


Response.Redirect ("nextpage.aspx", false);
 catch (System.Threading.ThreadAbortException e){
 throw;
 }
  You can then call this function through other functions or events to download files on the server 
 protected void btnOutput_Click(object sender, EventArgs e){
 try{
  string strPath = Server.MapPath("/") + "Download// Basic Student Information Template .xls";
  DownloadFile(strPath, " Basic Student Information Template .xls");
 }
 catch (Exception exp){
  Response.Write("<script>alert(' The system has experienced the following error ://n" + exp.Message + "!//n Please contact the administrator as soon as possible .')</script>");
 }
 } 

From this event, we can see that the first parameter of DownloadFile function is the absolute path of the file, otherwise the program will report an error.


Related articles: