自己在项目中,做了一个文件下载功能模块。在获得服务器端生成的文件名createdFileName之后,需要将该文件以另存为的方式自动提供保存。开始在Response.AppendHeader方法中,直接将createdFileName作为参数,导致系统布置服务器在服务器端时,下载出现文件名乱码的问题。现修改为Server.UrlPathEncode方法对文件名进行编码,实现了文件名的正确显示。

 
Title

string createdFileName = Request.QueryString["createdFileName"];
      
   //文件生成后,直接弹出另存为窗口
   Response.ContentType = "application/octet-stream";
   
   //设置弹出保存窗口中默认文件名为新文件名称
   
   HttpContext context = HttpContext.Current;
   string filePath = context.Request.ApplicationPath;
   filePath = context.Server.MapPath(filePath);
    filePath = filePath + "/downloadFile/";
   Response.AppendHeader("Content-Disposition","attachment; filename="+Server.UrlPathEncode(createdFileName));
   
   stbprpFilePath = filePath + createdFileName;
   try
   {
         Response.TransmitFile(stbprpFilePath);
         Response.End();
   
    
   }
   catch(ThreadAbortException ex)
   {
         Console.Write(ex.Message);
   }