Asp.net生成htm靜態檔案的兩種途徑(轉)
阿新 • • 發佈:2019-02-06
{
12//原始碼是替換掉模板中的特徵字元
13
14string mbPath =Server.MapPath("template.htm");
15 Encoding code = Encoding.GetEncoding("gb2312");
16 StreamReader sr =null;
17 StreamWriter sw =null;
18string str =null;
19
20//讀取
21try
22{
23 sr =new StreamReader(mbPath, code);
24 str = sr.ReadToEnd();
25
26 }
27catch (Exception ex)
28{
29throw ex;
30 }
31finally
32{
33 sr.Close();
34 }
35
36//根據時間自動重新命名,副檔名也可以自行修改
37string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") +".htm";
38 str = str.Replace( "$title$", txtTitle.Text);//替換Title
39 str = str.Replace("$content$", txtContent.Text);//替換content
40
41//生成靜態檔案
42try
43{
44 sw =new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
45 sw.Write(str);
46 sw.Flush();
47
48 }
49catch (Exception ex)
50{
51throw ex;
52 }
53finally
54{
55 sw.Close();
56 Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已經生成,儲存在htm資料夾下!");
57 }
58
59
60 }
12//原始碼是替換掉模板中的特徵字元
13
14string mbPath =Server.MapPath("template.htm");
15 Encoding code = Encoding.GetEncoding("gb2312");
16 StreamReader sr =null;
17 StreamWriter sw =null;
18string str =null;
19
20//讀取
21try
22{
23 sr =new StreamReader(mbPath, code);
25
26 }
27catch (Exception ex)
28{
29throw ex;
30 }
31finally
32{
33 sr.Close();
34 }
35
36//根據時間自動重新命名,副檔名也可以自行修改
37string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") +".htm";
38 str = str.Replace(
39 str = str.Replace("$content$", txtContent.Text);//替換content
40
41//生成靜態檔案
42try
43{
44 sw =new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
45 sw.Write(str);
46 sw.Flush();
47
48 }
49catch
50{
51throw ex;
52 }
53finally
54{
55 sw.Close();
56 Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已經生成,儲存在htm資料夾下!");
57 }
58
59
60 }