標籤:style blog http color 檔案 os
response 後重新整理頁面
先要求點按鈕組建檔案,同時按鈕變灰,檔案產生好直接開啟該檔案,按鈕可用。
一,給按鈕增加點擊後變灰,頁面重新整理變可用屬性。
開啟檔案代碼如下:
//寫出檔案 System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312"); HttpResponse response = HttpContext.Current.Response; response.HeaderEncoding = encoding; response.Charset = encoding.HeaderName; response.Clear(); response.ContentEncoding = encoding; response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(pdfFile.Name)); response.AddHeader("Content-Length", pdfFile.Length.ToString()); response.ContentType = "application/pdf"; response.WriteFile(pdfFile.FullName); HttpContext.Current.ApplicationInstance.CompleteRequest();開啟檔案
要彈出檔案,頁面請求會立刻結束。所以這時你把按鈕的任何控制都不會執行。
二,aspx頁面:增加一個iframe
<iframe id="ifrm_popMessage" src="" frameborder="0" scrolling="no" align="middle" style="position: absolute; z-index: 100; display: none; border: 1px #62A3D2 solid;"> </iframe>
三,aspx頁面:增加JS指令碼:
////開啟檔案 function OpenPdfFile() { document.getElementById(‘ifrm_popMessage‘).src = "OpenPdfFile.aspx?Rnd" + Math.random(); }
四,組建檔案結束後增加如下代碼:
ScriptManager.RegisterStartupScript(this.Page, ClientScript.GetType(), "myscript", "<script>OpenPdfFile();</script>", false);
五,OpenPdfFile 頁面代碼:
/// <summary>/// 用來產生PDF檔案/// </summary>public partial class Admin_IEData_OpenPdfFile : System.Web.UI.Page{ /// <summary> /// 頁面載入 /// </summary> protected void Page_Load(object sender, EventArgs e) { DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/File/")); string strFileName = string.Format("{0}({1}){2}.pdf", di.FullName, SessionOperate.getLoginName(), "ddpdf"); FileInfo pdfFile = new System.IO.FileInfo(strFileName); WriteOutFile(pdfFile); } #region 寫出檔案 /// <summary> /// 寫出檔案 /// </summary> /// <param name="pdfFile">檔案對象</param> /// <returns>返回是否成功</returns> protected bool WriteOutFile(FileInfo pdfFile) { //寫出檔案 System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312"); HttpResponse response = HttpContext.Current.Response; response.HeaderEncoding = encoding; response.Charset = encoding.HeaderName; response.Clear(); response.ContentEncoding = encoding; response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(pdfFile.Name)); response.AddHeader("Content-Length", pdfFile.Length.ToString()); response.ContentType = "application/pdf"; response.WriteFile(pdfFile.FullName); HttpContext.Current.ApplicationInstance.CompleteRequest(); return true; } #endregion}Iframe頁面代碼