将页面下载到本地。

将系统中的页面下载到本地是我们常遇到的需求,古在此在基本的方法写下来,以备后用。

 

 

在页面初始化完成后,在PageLOad的最后加入以下代码即可。

 

Response.Clear();
                Response.Buffer = true;
                //Response.Charset = "GB2312";
                Response.Charset = "utf-8";
               // Response.ContentType = "application/ms-excel";//octet-stream
                Response.ContentType = "application/octet-stream";
                Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
               
                string filename = System.Web.HttpUtility.UrlEncode(lblName1.Text.Trim(), System.Text.Encoding.UTF8);
                string file = "attachment; filename=" + filename + "" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".html";

      //此后缀名和可以修改为word或xls。
                Response.AppendHeader("Content-Disposition", file);

                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

                this.EnableViewState = false;
                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Page.RenderControl(oHtmlTextWriter);
                Response.Write(oStringWriter.ToString());
                Response.End();

 

posted @ 2010-06-10 16:18  autumnyuhe  阅读(468)  评论(1编辑  收藏  举报