码迷,mamicode.com
首页 > Web开发 > 详细

生成html文件

时间:2015-04-14 16:37:20      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

第一步:建立一个MbPage.html页面

第二步:后台生成

  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string mbPath = context.Server.MapPath("MbPage.html");
            Encoding code = Encoding.GetEncoding("gb2312");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = null;

            //读取
            try
            {
                sr = new StreamReader(mbPath, code);
                str = sr.ReadToEnd();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sr.Close();
            }

            //根据时间自动重命名,扩展名也可以自行修改
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
            str = str.Replace("$title$", "111");//替换Title
            str = str.Replace("$content$","2222");//替换content

            //生成静态文件
            try
            {
                sw = new StreamWriter(context.Server.MapPath("htm/") + fileName, false, code);
                sw.Write(str);
                sw.Flush();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.Close();
               context.Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
            }
            context.Response.End();
        }

 

生成html文件

标签:

原文地址:http://www.cnblogs.com/115FXC/p/4424953.html

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