Home  >  Article  >  Backend Development  >  ASP.NET handles the problem of downloading files from the server

ASP.NET handles the problem of downloading files from the server

高洛峰
高洛峰Original
2017-02-20 17:07:281432browse

This article mainly introduces the problem of downloading files from the server in ASP.NET. It has a very good reference value. Let’s take a look at it with the editor.

Assume that there is a name in the root directory of the server. It is the Download folder. This folder stores some files provided for download by 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;
   // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
   // 添加头信息,指定文件大小,让浏览器能够显示下载进度
   Response.AddHeader("Content-Length", file.Length.ToString());
   // 指定返回的是一个不能被客户端读取的流,必须被下载
   Response.ContentType = "application/ms-excel";
   // 把文件流发送到客户端
   Response.WriteFile(file.FullName);
   // 停止页面的执行
   //Response.End();  
HttpContext.Current.ApplicationInstance.CompleteRequest();
  }
  catch (Exception ex){
   Response.Write("<script>alert(&#39;系统出现以下错误://n" + ex.Message + "!//n请尽快与管理员联系.&#39;)</script>");
  }
 }

This function is a group program for the download function, where path is the file The absolute path (including file name), name is the file name, and this program can be run. If you replace HttpContext.Current.ApplicationInstance.CompleteRequest(); with Response.End();, the following error will occur: Exception: Because the code has been optimized or the native framework is located on the call stack, the value of the expression cannot be calculated. However, this error will not affect the running of the program, although try can catch this exception (not sure why)

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

The solutions 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 code execution for 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 Response Internal call to .End. For example:

Response.Redirect ("nextpage.aspx", false);
 catch (System.Threading.ThreadAbortException e){
  throw;
 }
 接下来就可以通过其他函数或者事件调用这个函数来下载服务器上的文件了
 protected void btnOutput_Click(object sender, EventArgs e){
  try{
   string strPath = Server.MapPath("/") + "Download//学生基本信息模版.xls";
   DownloadFile(strPath, "学生基本信息模版.xls");
  }
  catch (Exception exp){
   Response.Write("<script>alert(&#39;系统出现以下错误://n" + exp.Message + "!//n请尽快与管理员联系.&#39;)</script>");
  }
 }

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

For more articles related to ASP.NET implementation of downloading files from the server, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn