http://blog.163.com/red_guitar@126/blog/static/117206128201123845276/ 思路,在模態視窗頁面放一個影藏的iframe。
首先,我們建立一個模態視窗,在模態視窗a.aspx Java代碼
- <!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>
<!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代碼
- protected void ibDownLoad_Click(object sender, EventArgs e)
- {
- string filePath = Server.MapPath("\\PDF\\a.txt");
- download.Attributes["src"] = "DownLoad.aspx?filePath=" + filePath;
- }
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代碼
- 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();
- }
- }
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