`
caozuiba
  • 浏览: 899686 次
文章分类
社区版块
存档分类
最新评论

生成静态文件的新闻系统核心代码

 
阅读更多
<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace makehtmlfile
{
/// <summary>
/// makeallfiles 的摘要说明。
/// </summary>
public class makeallfiles : System.Web.UI.Page
{
public string strcon;
public OleDbConnection conn;
public string strSQL;

private void Page_Load(object sender, System.EventArgs e)
{

InitialPages();// 在此处放置用户代码以初始化页面
}

public void InitialPages()
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";//连接字符窜// 在此处放置用户代码以初始化页面
strSQL = "select id,class1id,class2id from news order by id desc";
MakeAreaForShow();
ReadNewsForWriteFileUserDataReader(); //同过DataReader来读取数据,
//ReadNewsForWriteFileUserDataSet(); //将数据直接挂入DataSet中来读取,
}
/// <summary>
/// 用来产生循环显示页面的区域,装载生成HTML页的ASPX页面的区域
/// </summary>
public void MakeAreaForShow()
{
Response.Write("<span id=showImport></span>");
Response.Write("<IE:Download ID='oDownload' STYLE='behavior:url(#default#download)'/>");
}

/// <summary>
/// 通过DATAREADER来读取数据
/// </summary>
public void ReadNewsForWriteFileUserDataReader()
{
int num = 0 ;
string newsid = null;
string class1id = null;
string class2id = null;
OleDbDataReader dr = null;
OleDbConnection conn = new OleDbConnection(strcon);
conn.Open();
OleDbCommand mycommand = new OleDbCommand(strSQL,conn);
dr = mycommand.ExecuteReader();
while(dr.Read())
{
newsid = dr["id"].ToString();
class1id = dr["class1id"].ToString();
class2id = dr["class2id"].ToString();
WriteJScript(newsid,class1id,class2id);
num++;
}
dr.Close();
conn.Close();
Response.Write(num.ToString());

}

/// <summary>
/// 通过DATASET来读取数据
/// </summary>
public void ReadNewsForWriteFileUserDataSet()
{
DataSet ds = new DataSet();
int num = 0 ;
string newsid = null;
string class1id = null;
string class2id = null;

OleDbConnection conn = new OleDbConnection(strcon);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,conn);
da.Fill(ds,"news");
conn.Close();
num = ds.Tables["news"].Rows.Count;
foreach(DataRow dr in ds.Tables["news"].Rows)
{
newsid = dr["id"].ToString();
class1id = dr["class1id"].ToString();
class2id = dr["class2id"].ToString();
WriteJScript(newsid,class1id,class2id);
}
ds = null;
Response.Write(num.ToString());

}

public void WriteJScript(string newsid,string class1id,string class2id)
{
Response.Write("<script>");
Response.Write("function onDownloadDone(downDate)");
Response.Write("{");
Response.Write("showImport.innerHTML=downDate");
Response.Write("}");
Response.Write("oDownload.startDownload('makefile2.aspx?id=");
Response.Write(newsid);
Response.Write("&class1id=");
Response.Write(class1id);
Response.Write("&class2id=");
Response.Write(class2id);
Response.Write("',onDownloadDone)");
Response.Write("</script>");
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace makehtmlfile
{
/// <summary>
/// makefile2 的摘要说明。
/// </summary>
public class makefile2 : System.Web.UI.Page
{
public string strcon;
public OleDbConnection conn;

public string class1id;
public string class2id;
//***********************************
public string previd;
public string prevtitle;
public string nextid;
public string nexttitle;
//***********************************
public string newstitle;
public string newscontent;
public string newsdate;
public string newsip;
public string newsid;
//************************************

private void Page_Load(object sender, System.EventArgs e)
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";//连接字符窜// 在此处放置用户代码以初始化页面
if(Request.Params["id"]!=null&&Request.Params["class1id"]!=null&&Request.Params["class2id"]!=null)
{
InitialPages();

}// 在此处放置用户代码以初始化页面
}

public void InitialPages()
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";

if(Request.Params["id"]!=null)
{
newsid = Request.Params["id"].ToString();
}
if(Request.Params["class1id"]!=null)
{
class1id = Request.Params["class1id"].ToString();
}
if(Request.Params["class2id"]!=null)
{
class2id = Request.Params["class2id"].ToString();
}
ReadDataBase(newsid,class2id);
MakeHtmlFile(newsid,class1id,class2id);
}

/// <summary>
/// 读写同一分类中,上一篇,和下一篇文章
/// </summary>
/// <param name="inputid"> 该文章ID</param>
/// <param name="class2id">该文章所属分类ID</param>
public void ReadPrevAndNext(string inputid,string class2id)
{
int id = int.Parse(inputid);
string strPrevSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id<"+id+" order by id desc";
string strNextSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id>"+id+" order by id asc";
OleDbDataReader datar = null;
OleDbConnection con = new OleDbConnection(strcon);
con.Open();
OleDbCommand newcommand = new OleDbCommand(strPrevSQL,con);
datar = newcommand.ExecuteReader();
while(datar.Read())
{
previd = datar["id"].ToString();
prevtitle = datar["newstitle"].ToString();
}
datar.Close();
newcommand.CommandText = strNextSQL ;
datar = newcommand.ExecuteReader();
while(datar.Read())
{
nextid = datar["id"].ToString();
nexttitle = datar["newstitle"].ToString();
}
con.Close();

}

/// <summary>
/// 将文章信息从库中读出,并将准备生成的HTML文件路径写入库中
/// </summary>
/// <param name="inputid"></param>
/// <param name="class2id"></param>
public void ReadDataBase(string inputid,string class2id)
{
string filename_w = MakeFileName(class1id,class2id,newsid)+".htm";

ReadPrevAndNext(inputid,class2id); //读取下一篇和上一篇的信息。


OleDbConnection mycon = new OleDbConnection(strcon); //打开数据库连接
mycon.Open();

int id = int.Parse(inputid);
string strSQL = "select * from news where id="+id;
OleDbDataReader dr = null;
OleDbCommand mycommand = new OleDbCommand(strSQL,mycon);
dr = mycommand.ExecuteReader();
while(dr.Read())
{
newstitle = dr["newstitle"].ToString();
newscontent = dr["newscontent"].ToString();
newsdate = dr["newsdate"].ToString();
newsip = dr["newsip"].ToString();
}
dr.Close();
mycommand.CommandText = "update news set url='"+ filename_w +"' where id="+int.Parse(inputid); //将生成的文件路径写入库中,以遍在生成分类页中方便使用
mycommand.ExecuteNonQuery();
mycon.Close();

}


/// <summary>
/// 生成目标目录和文件,主要用来生成不同分类的目录
/// </summary>
/// <param name="inputstr"></param>
/// <returns></returns>
public string MakeCatalogName(string class1,string class2) //生成目标目录文件
{
string namestr = "Article";
string rootstr = Server.MapPath(".").ToString();
string class1str = rootstr + "//" + namestr + "_" + class1 + "//";
string class2str = rootstr + "//" + namestr + "_" + class1 + "//" + namestr + "_" + class2 + "//";
if(!Directory.Exists(class1str))
{
Directory.CreateDirectory(class1str);
}
if(!Directory.Exists(class2str))
{
Directory.CreateDirectory(class2str);
}
//创建目标文件夹
return class2str;
}

/// <summary>
/// 根据文章分类和ID生成文件名
/// </summary>
/// <param name="class1id"></param>
/// <param name="class2id"></param>
/// <param name="nid"></param>
/// <returns>返回文件名</returns>
public string MakeFileName(string class1,string class2,string id) //生成文件名,能够生成上下篇
{
string myclass2id = class2;
string myclass1id = class1;
string s = DateTime.Now.Year.ToString()
+DateTime.Now.Month.ToString()
+DateTime.Now.Day.ToString()
+"_"
+myclass1id
+"_"
+myclass2id //父类ID
+"_"
+id; //新闻ID
return s;
}

/// <summary>
/// 生成HTML文件
/// </summary>
/// <param name="nid">文章ID号</param>
public void MakeHtmlFile(string nid,string cla1id,string cla2id) // MakeHtmlFile(string nid,string cla1id,string cla2id,string filetemp) 用于区分不同的摸班
{
string file_path = Server.MapPath ("template/news_mb.htm");
string desfilename = MakeFileName(cla1id,cla2id,nid)+".htm";
string desfile = MakeCatalogName(cla1id,cla2id)+MakeFileName(cla1id,cla2id,nid)+".htm";
string prevurl = MakeFileName(cla1id,cla2id,previd)+".htm"; //根据分类和ID生成上下篇的文件连接名
string nexturl = MakeFileName(cla1id,cla2id,nextid)+".htm"; //下篇
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");

StreamReader srd = null; //读
StreamWriter swr = null; //写

string strFile = null ; //字符串

try
{
srd = new StreamReader(file_path, code);
strFile = srd.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
srd.Close();
}

strFile = strFile.Replace("$title$",newstitle);
strFile = strFile.Replace("$content$",newscontent);
strFile = strFile.Replace("$date$",newsdate);
strFile = strFile.Replace("$ip$",newsip);
strFile = strFile.Replace("$prev$",prevtitle);
strFile = strFile.Replace("$next$",nexttitle);
strFile = strFile.Replace("$prevurl$",prevurl);
strFile = strFile.Replace("$nexturl$",nexturl);

try
{
swr = new StreamWriter(desfile,false,code);
swr.Write(strFile);
swr.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
swr.Close();
}
if(srd!=null)
{
srd.Close();
}
Response.Write(desfilename);

}

public void Open()
{
if(conn==null)
{
conn = new OleDbConnection(strcon);
conn.Open();
}
}
public void Close()
{
if(conn!=null)
{
conn.Close();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

分享到:
评论

相关推荐

    PHP生成静态页面小程序 v1.1.zip

    因为考虑到PHP新手用户针对PHP简单源码的需求,这个版本除了在生成静态原理核心代码上有所加强外,还新加了分页系统、后台登陆系统、验证码功能,独立密码修改、注销代码、前台调用新闻分类等针对PHP新手操作性比较...

    网趣网上购物系统HTML静态版 2016.rar

    修复商品展示页导航错位修复静态生成配置文件核心算法修复商品分类页分页功能代码修复购物车中商品增减数量功能修复订单打印文本输出功能修复新订单邮件通知自定度功能修复网银在线支付接口更新包修复新闻系统代码...

    网趣网上购物系统HTML静态版 v2016 购物网站源码

    网趣购物系统静态版支持网站一键静态生成,采用动态进度条模式生成静态,生成过程更加清晰明确,商品管理上增加淘宝数据包导入功能,与淘宝数据同步更新!采用领先的AJAX+XML相融技术,速度更快更高效!系统进行了...

    uClinux源代码中Make文件完全解析

    内容简介: 在Linux或μClinux源代码中,Make文件是一种重要的文件,它担当着编译生成系统目标代码的重任。本书立足于μClinux源代码,通过分析其中的Make文件,以及与这些Make文件密切相关的部分源代码,给出一...

    htaccess文件在线生成器 v2.0.zip

    htaccess在线生成编辑器是一个免费的在线生成工具,它能够为你的网站快速生成一个 .htaccess 文件。使用非常简单,在页面左侧选择类别,再按照提示在右侧输入相关信息。最后把生成的代码以“.htaccess”的文件名保存...

    PHP生成静态页面小程序 1.1

    因为考虑到PHP新手用户针对PHP简单源码的需求,这个版本除了在生成静态原理核心代码上有所加强外,还新加了分页系统、后台登陆系统、验证码功能,独立密码修改、注销代码、前台调用新闻分类等针对PHP新手操作性比较...

    PHP生成静态页面小程序官方版 v1.1

    因为考虑到PHP新手用户针对PHP简单源码的需求,这个版本除了在生成静态原理核心代码上有所加强外,还新加了分页系统、后台登陆系统、验证码功能,独立密码修改、注销代码、前台调用新闻分类等针对PHP新手操作性比较...

    OK3W新闻发布系统静态 v6.0 增强版.zip

    3、此次修正主要为增加足够多的广告位,修复无法生成静态页面问题,修正程序现有bug,以及进行必要的优化修改,OK3W新闻发布系统6.0版本已知错误均已修复,此次修复版本暂命名为“OK3W6.0静态增强版”,站长QQ ...

    代码审计以及代码审计工具的定义,意义所在

    这样的工具虽然可以生成各种漂亮的报告,但是真正懂代码的人,会很容易发现这样的代码审计报告只能走过场,对于发现和修复代码中的潜在风险,改进代码质量是无好处的,消耗开发人员的时间排除各种误报,肯定也会得到...

    图片网站源码分享

    (2) 首页部分文字和广告位需要手动修改跟目录下的index.asp文件,由于系统问题,建议用户不要修改静态文件的保存目录和文件名前缀,涉及修改的文件较多,我已经给大家 设置好了,除了首页不生成静态,其他全部内容...

    基于springboot的设备管理系统+源代码+文档说明

    - 提供了代码生成器,只需编写30%左右代码,其余的代码交给系统自动生成,可快速完成开发任务 - 后台系统支持MySQL、Oracle、SQL Server、PostgreSQL等主流数据库,客户端系统仅支持MySQL **具有如下特点** -...

    YothSHOP 开源商城系统 v1.0.rar

    YothSHOP是优斯科技鼎力打造的一款asp开源商城系统,支持access和Sql server切换,完善的会员订单管理,全站生成静态html文件,SEO优化效果极佳,后台XP模式和普通模式随意切换,极易操作,欢迎使用! 若大家测试和...

    Windows应用程序捆绑核心编程光盘代码

    8.3.2 向PE文件中静态注入代码的完整实例 183 8.4 如何实现文件脱壳 191 8.5 本章小结 192 参考文献 192 第9章 应用程序的动态挂钩 193 9.1 动态挂钩概述 193 9.2 使用Windows钩子函数挂钩 194 9.2.1 Windows...

    风讯站点管理系统2004.I.0225(第2版)

    支持目前V2004.I所有功能,另外支持: ?支持无限分类 ?加强图片功能 ...优化核心代码 ?完全静态生成 ?所有类别及首页实现了标签化管理 ?远程抓取图片 ?新闻采集功能 ?块操作功能 ?数据库统计功能

    华美卷皮淘宝客-自动生成商品HTML插件 v1.0

    2、打开后台,找到:系统设置--》核心设置--》SEO设置,在“URL静态化”里,设置URL模式为:REWRITE模式,URL后缀:.html,参数分隔符:/3、将本插件所有文件夹和文件上传到您的网站根目录。运行插件程序:http://...

    华美卷皮淘宝客-自动生成商品HTML插件 v1.0.zip

    2、打开后台,找到:系统设置--》核心设置--》SEO设置,在“URL静态化”里,设置URL模式为:REWRITE模式,URL后缀:.html,参数分隔符:/ 3、将本插件所有文件夹和文件上传到您的网站根目录。运行插件程序:http://...

    华美卷皮淘宝客-自动生成商品HTML插件 1.0

    2、打开后台,找到:系统设置--》核心设置--》SEO设置,在“URL静态化”里,设置URL模式为:REWRITE模式,URL后缀:.html,参数分隔符:/ 3、将本插件所有文件夹和文件上传到您的网站根目录。运行插件程序:http://...

    织梦手机助手(PC WAP完美同步) v2.0.zip

    织梦手机助手的优点: 1、模板制作简单,通用织梦标签 ...系统 》 系统基本参数 》 核心设置 》 **支持多站点**选择“是” 》 确定 织梦手机助手(PC WAP完美同步) 页面展示: 相关阅读 同类推荐:站长常用源码

    QVMS电影系统 v1.0 build 090828.rar

    QVMS视频分享专家显著功能与特点! ... 本程序ASP ACC/ASP SQL完全免费开源,软件的个性化订制;采集规则的个性化订制及模板的订制采用收取服务费模式使得系统可以更好的发展...* 优化了核心代码 解决卡巴提示木马问题

    代码语法错误分析工具pclint8.0

    代码静态分析工具PC-LINT安装配置 PC-Lint是C/C++软件代码静态分析工具,你可以把它看作是一种更加严格的编译器。它不仅可以检查出一般的语法错误,还可以检查出那些虽然符合语法要求但不易发现的潜在错误。 ...

Global site tag (gtag.js) - Google Analytics