最近在看用asp。net生成静态页面的方法,用来学习学习,特此转来自己的空间
public class News
...{
private const string C_ERR_MSG = "You have no rights!";
private const string C_CHK_NAME = "NewsManage";
private static string C_DIR = ConfigurationManager.AppSettings["NewsHtml"];
public event CreateHtmlHandler CompleteAFile;
public static int Create(string AdminName,NewsInfo news)
...{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
news.HtmlFile =FileOperator.GetRandFileName(".html");
int ret= NewsDAL.Create(news);
CreateHtmlFile(news);
return ret;
}
public static int DeleteByIdList(string AdminName,string IdList)
...{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
DataTable dt = SelectHtmlFileByIdList(IdList).Tables[0];
string fileName = "";
foreach (DataRow dr in dt.Rows)
...{
if (!(dr[0] is DBNull))
...{
fileName = dr[0].ToString();
if(string.IsNullOrEmpty(fileName))
...{
fileName = FileOperator.MapPath(C_DIR + fileName);
File.Delete(fileName);
}
}
}
return NewsDAL.DeleteByIdList(IdList);
}
public static int DeleteByNewsID(string AdminName, int NewsID)
...{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
string fileName = SelectHtmlFile(NewsID);
if (!string.IsNullOrEmpty(fileName))
File.Delete(HttpContext.Current.Server.MapPath(C_DIR + fileName));
return NewsDAL.DeleteByNewsID(NewsID);
}
public static DataSet QueryPage(int ClassID, string Title, int PageSize, int PageIndex)
...{
return NewsDAL.QueryPage(ClassID,Title,PageSize,PageIndex);
}
public static int QueryPageCount(int ClassID, string Title)
...{
return NewsDAL.QueryPageCount( ClassID, Title);
}
public static DataSet QueryTopNews(int ClassID, int Num, bool OrderType)
...{
return NewsDAL.QueryTopNews(ClassID, Num, OrderType);
}
public static string SelectHtmlFile(int NewsID)
...{
return NewsDAL.SelectHtmlFile(NewsID);
}
public static DataSet SelectHtmlFileByIdList(string IdList)
...{
return NewsDAL.SelectHtmlFileByIdList(IdList);
}
public static int Update(string AdminName,NewsInfo news)
...{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
int ret= NewsDAL.Update(news);
CreateHtmlFile(news);
return ret;
}
public static DataSet QueryPageWithContent(int ClassID, string Title, int PageSize, int PageIndex)
...{
return NewsDAL.QueryPageWithContent( ClassID, Title, PageSize, PageIndex);
}
//这里
public void CreateHtmlFile(int startPage, int endPage,int pageSize,int ClassID)
...{
int total = QueryPageCount(ClassID, string.Empty);
total=(total % pageSize)==0 ? total / pageSize : (total /pageSize) +1;
if (total < endPage)
endPage = total;
string fileName = "";
string HtmlFile = "";
int rTotal = (endPage - startPage + 1) * pageSize; //总记录数
for (int i = startPage; i <= endPage; i++)
...{
DataTable dt = QueryPageWithContent(ClassID, string.Empty, pageSize, i).Tables[0];
int curR = (i-startPage) * pageSize;
foreach (DataRow dr in dt.Rows)
...{
if (!(dr["HtmlFile"] is DBNull))
...{
HtmlFile = dr["HtmlFile"].ToString();
if (string.IsNullOrEmpty(HtmlFile))
...{
HtmlFile = FileOperator.GetRandFileName(".html");
}
fileName = XMLData.GetFileNameByID(TemplateNode.News, (int)dr["TemplateID"]);
string tempContent = FileOperator.LoadFileWithCache(fileName);
tempContent = tempContent.Replace("{$NewsTitle$}", dr["title"].ToString());
tempContent = tempContent.Replace("{$NewsContent$}", dr["content"].ToString());
tempContent = tempContent.Replace("{$NewsAddTime$}", ((DateTime)dr["AddTime"]).ToString("yyyy-MM-dd"));
curR++;
FileOperator.SaveFile(FileOperator.MapPath(C_DIR + HtmlFile), tempContent);
if (CompleteAFile != null)
CompleteAFile(HtmlFile, string.Empty,(curR * 100)/rTotal);
}
}
}
}
private static void CreateHtmlFile(NewsInfo news)
...{
string fileName = XMLData.GetFileNameByID(TemplateNode.News, news.TemplateID);
string tempContent = FileOperator.LoadFileWithCache(fileName);
tempContent = tempContent.Replace("{$NewsTitle$}", news.Title);
tempContent = tempContent.Replace("{$NewsContent$}", news.Content);
tempContent=tempContent.Replace("{$NewsAddTime$}",news.AddTime.ToString("yyyy-MM-dd"));
FileOperator.SaveFile(HttpContext.Current.Server.MapPath(C_DIR + news.HtmlFile),tempContent);
}
}
} public partial class _Default : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void NavigateButton1_Click(object sender, EventArgs e)
...{
WDFROG.BLL.News.DeleteByIdList(Admin.AdminName, "1,2,3,4,5,6");
}
protected void Button1_Click(object sender, EventArgs e)
...{
WDFROG.BLL.News objNews = new News();
objNews.CompleteAFile += new CreateHtmlHandler(CreateAFile);
Response.Buffer = false;
Response.Write("<div style="width: 100px; height: 25px;background-color:yellow;" Id="cp">_</div>" + Environment.NewLine);
objNews.CreateHtmlFile(1, 100, 30, -1);
Response.Write("<script>cp.style.visibility='hidden'</script>");
}
protected void CreateAFile(string fileName, string msg, int percent)
...{
this.Response.Write(string.Format("<script> cp.innerHTML='Complete {0}%'; </script>", percent ));
Response.Flush();
模板页
<HEAD>
<title>ShowArticle</title>
</HEAD>
<body>
biaoti
<br> __
content<br>
author
</body>
</HTML>
/以一个文章系统为例来说明
public static bool WriteFile(string strText, string strContent, string strAuthor)
{ _
string path = HttpContext.Current.Server.MapPath("/news/");//指定路径
Encoding code = Encoding.GetEncoding("gb2312"); //编码方式
//读取模板文件---第一步
string temp = HttpContext.Current.Server.MapPath("/news/text.html");
streamReader sr = null;
streamWriter sw = null;
string str = "";
try
{
sr = new streamReader(temp, code);//以gb2312的编码方式将模板文件temp 读到sr
str = sr.ReadToEnd();//读取全部文件内容到str
}
catch (Exception exp) //出错处理
{
HttpContext.Current.Response.Write(exp.message);
HttpContext.Current.Response.End();
sr.close();
}
// 这时,模板文件已经读入到名称为str的变量中
string HtmlFileName = DateTime.Now.ToString("yyyymmddhhmmss") + "html";
//替换内容----第二步
str = str.Replace("ShowArticle", strArticle);//ShowArticle中的内容被 strArticle替换;以下同
str = str.Replace("biaoti", strArticle);
str = str.Replace("content", strContent);
str = str.Replace("Author", strAuthor);
// 写文件----第三步
try
{ sw = new StreamWriter(path,HtmlFileName,false,code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)//出错处理
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.close();
}
return true;
}
}
来源http://www.cnblogs.com/yhb199/archive/2008/10/28/1321140.html