English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Problem Handling of Downloading Files from Server with ASP.NET

Assuming there is a folder named Download under the root directory of the server, which contains some files provided for download by the referencing program

 public void DownloadFile(string path, string name){
 try{
  System.IO.FileInfo file = new System.IO.FileInfo(path);
  Response.Clear();
  Response.Charset = "GB"2312";
  Response.ContentEncoding = System.Text.Encoding.UTF8;
  // adding header information for "file download"/specifying the default file name in the "Save As" dialog box
  Response.AddHeader("Content"-Disposition", "attachment; filename=" + Server.UrlEncode(name));
  // adding header information to specify the file size so that the browser can display the download progress
  Response.AddHeader("Content"-Length", file.Length.ToString());
  // specifying that the return is 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 the execution of the page
  //Response.End(); 
HttpContext.Current.ApplicationInstance.CompleteRequest();
 }
 catch (Exception ex){
  Response.Write("<script>alert('The following error occurred in the system://n" + ex.Message + "!//nPlease contact the administrator as soon as possible.')</script>
 }
 }

This function is a group program for the download feature, where path is the absolute path of the file (including the filename), and name is the filename. This program is able to run. If you replace HttpContext.Current.ApplicationInstance.CompleteRequest(); with Response.End();, the following error will occur: Exception: Due to code optimization or the local framework being above the call stack, the value of the expression cannot be calculated. However, this error will not affect the operation of the program, although try can catch this exception (for some reason)

I found some reasons for this problem online: If you use the Response.End, Response.Redirect, or Server.Transfer methods, a ThreadAbortException exception will occur. You can use try-The catch statement catches this exception. The Response.End method terminates the page execution and switches the execution to the application's event pipeline Application_EndRequest event. The code lines after Response.End are not executed. This problem occurs in Response.Redirect and Server.Transfer methods because both of these methods internally call Response.End.

The provided solutions include:

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

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

For Response.Redirect, use the overloaded Response.Redirect(String url, bool endResponse) and pass 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;
 }
 Then you can call this function to download files on the server through other functions or events.
 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 following error occurred in the system://n" + exp.Message + "!//nPlease contact the administrator as soon as possible.')</script>
 }
 } 

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

That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to support the呐喊 tutorial more!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)

You May Also Like