asp.net模态窗口下载文件解决方案

 
思路,在模态窗口页面放一个影藏的iframe。
首先,我们新建一个模态窗口,在模态窗口a.aspx
Java代码 复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">   
  4. <head runat="server">   
  5.     <base target="_self"/>   
  6.     <title></title>   
  7. </head>   
  8. <body>   
  9.     <form id="form1" runat="server">   
  10.     <center>   
  11.                     <asp:LinkButton ID="ibDownLoad" runat="server" onclick="ibDownLoad_Click">点击这里下载文件</asp:LinkButton>   
  12.     </center>   
  13.     <iframe id="download" runat="server" style="display:none;"></iframe>   
  14.     </form>   
  15. </body>   
  16. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    <html xmlns="http://www.w3.org/1999/xhtml">  <head runat="server">      <base target="_self"/>      <title></title>  </head>  <body>      <form id="form1" runat="server">      <center>                      <asp:LinkButton ID="ibDownLoad" runat="server" onclick="ibDownLoad_Click">点击这里下载文件</asp:LinkButton>      </center>      <iframe id="download" runat="server" style="display:none;"></iframe>      </form>  </body>  </html>


后台代码:
Java代码 复制代码
  1. protected void ibDownLoad_Click(object sender, EventArgs e)   
  2.         {   
  3.             string filePath = Server.MapPath("\\PDF\\a.txt");   
  4.             download.Attributes["src"] = "DownLoad.aspx?filePath=" + filePath;   
  5.          }  
protected void ibDownLoad_Click(object sender, EventArgs e)          {              string filePath = Server.MapPath("\\PDF\\a.txt");              download.Attributes["src"] = "DownLoad.aspx?filePath=" + filePath;           }

处理文件下载页面DownLoad.aspx,页面代码不写,在后台代码写:
Java代码 复制代码
  1. protected void Page_Load(object sender, EventArgs e)   
  2.         {   
  3.             string filePath = Request.QueryString["filePath"];   
  4.             //string filePath = Server.MapPath("\\PDF\\a.txt");   
  5.             if (File.Exists(filePath))   
  6.             {   
  7.                 FileInfo file = new FileInfo(filePath);   
  8.                 Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码   
  9.                 Response.AddHeader("Content-Disposition""attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码   
  10.                // Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);   
  11.                 Response.AddHeader("Content-length", file.Length.ToString());   
  12.                 Response.ContentType = "appliction/octet-stream";   
  13.                 Response.WriteFile(file.FullName);   
  14.                 Response.End();   
  15.             }   
  16.         }  
protected void Page_Load(object sender, EventArgs e)          {              string filePath = Request.QueryString["filePath"];              //string filePath = Server.MapPath("\\PDF\\a.txt");              if (File.Exists(filePath))              {                  FileInfo file = new FileInfo(filePath);                  Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码                  Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码                 // Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);                  Response.AddHeader("Content-length", file.Length.ToString());                  Response.ContentType = "appliction/octet-stream";                  Response.WriteFile(file.FullName);                  Response.End();              }          }

OK,完成,这样可以解决模态窗口点击下载链接没反应,或者弹出提示等等一些问题。
原文地址:https://www.cnblogs.com/fhuafeng/p/2804862.html