码迷,mamicode.com
首页 > 其他好文 > 详细

response 后刷新页面

时间:2014-07-01 20:13:00      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   文件   os   

response 后刷新页面

先要求点按钮生成文件,同时按钮变灰,文件生成好直接打开该文件,按钮可用。

一,给按钮增加点击后变灰,页面刷新变可用属性。

打开文件代码如下:

bubuko.com,布布扣
 //写出文件
        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 页面代码:

bubuko.com,布布扣
/// <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页面代码

 

 

 

response 后刷新页面,布布扣,bubuko.com

response 后刷新页面

标签:style   blog   http   color   文件   os   

原文地址:http://www.cnblogs.com/xbding/p/3816968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!