asp.net模態視窗下載檔案解決方案

來源:互聯網
上載者:User
 http://blog.163.com/red_guitar@126/blog/static/117206128201123845276/ 思路,在模態視窗頁面放一個影藏的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,完成,這樣可以解決模態視窗點擊下載連結沒反應,或者彈出提示等等一些問題。 轉自:http://269181927.javaeye.com/blog/865034

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.