Asp.net(C#)读取数据库并生成JS文件制作首页图片切

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 利用模板生成静态页面 /// </summary> /// <param>标题</param> /// <param>作者</param> /// <param>发布时间</param> /// <param>内容</param> /// <returns>生成页面名称</returns> public static string WriteFile(string strTitle, string strAuthor, string strDate, string strContent) { string path = HttpContext.Current.Server.MapPath("~/"); Encoding code = Encoding.GetEncoding("gb2312"); // 读取模板文件 string temp = HttpContext.Current.Server.MapPath("~/Template.html"); StreamReader sr = null; StreamWriter sw = null; string str = ""; try { sr = new StreamReader(temp, code); str = sr.ReadToEnd(); // 读取文件 } catch (Exception exp) { HttpContext.Current.Response.Write(exp.Message); HttpContext.Current.Response.End(); sr.Close(); } Random rd = new Random(); string strRd = rd.Next(0, 9999).ToString(); string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + strRd + ".html"; DateTime dtNow = DateTime.Now; // 替换内容 str = str.Replace("$biaoti", strTitle); str = str.Replace("$author", strAuthor); str = str.Replace("$datetime", strDate); str = str.Replace("$content", strContent); // 写文件 try { string pathUrl = path + dtNow.Year + "\\" + dtNow.Month + "\\" + dtNow.Day; if (!Directory.Exists(pathUrl)) { Directory.CreateDirectory(pathUrl); } sw = new StreamWriter(pathUrl + "\\" + 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 dtNow.Year.ToString() + "https://www.jb51.net/" + dtNow.Month.ToString() + "https://www.jb51.net/" + dtNow.Day.ToString() + "https://www.jb51.net/" + htmlfilename; } protected void Button1_Click(object sender, EventArgs e) { WriteFile("title" , "ttttttt" , "2011-09-27", "测试 <br>"); } }

Template.html

<table> <tr> <td>$biaoti</td> </tr> <tr> <td>作者:$author&nbsp;&nbsp;发布时间:$datetime</td> </tr> <tr> <td>$content</td> </tr> </table>

思路:首先读取数据库中图片,链接,说明文字等数据,然后将读取到的数据写入首页图片切换效果的JS文件。

下面代码实现向数据库中增加 图片、链接、说明文字等数据 和 生成JS文件

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.IO; using System.Text; public partial class Admin_Slide : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) { } protected void Add_Btn_Click(object sender, EventArgs e) //增加幻灯片,将信息写入数据库 string imgpath; imgpath = "../UpLoadFiles/SlideImg/" + ImgUp.FileName; ImgUp.SaveAs(Server.MapPath(imgpath)); MyOleDb mc = new MyOleDb(); mc.ConnOpen(); OleDbCommand cmd = new OleDbCommand("insert into SlideImg(lnk,pic,txt) values ('" + linkarea.Text.ToString() + "','" + imgpath + "','" + imgtitle.Text.ToString() + "');", mc.Conn); OleDbDataReader rdr = null; rdr = cmd.ExecuteReader(); mc.ConnClose(); } protected void MJS_Btn_Click(object sender, EventArgs e) //生成JS幻灯文件 string jsfile,jstemplete; string strlnk, strpic, strtxt; strlnk = null; strpic = null; strtxt = null; jsfile = Server.MapPath("~/Js/") + "SlideImg.js"; //JS文件路径 jstemplete = Server.MapPath("~/Js/") + "JsTemplete.js"; //JS文件模板路径 deljs(jsfile); //删除JS文件 MyOleDb mc = new MyOleDb(); mc.ConnOpen(); OleDbCommand cmd = new OleDbCommand("select top " + Img_Num.Text.ToString() + " * from SlideImg order by id desc", mc.Conn); OleDbDataReader rdr = null; rdr = cmd.ExecuteReader(); while (rdr.Read()) strlnk += rdr["lnk"].ToString() + "|"; strpic += rdr["pic"].ToString() + "|"; strtxt += rdr["txt"].ToString() + "|"; mc.ConnClose(); Encoding code = Encoding.GetEncoding("UTF-8"); StreamReader sr = null; StreamWriter sw = null; string str = ""; try sr = new StreamReader(jstemplete, code); str = sr.ReadToEnd(); // 读取文件 catch (Exception exp) HttpContext.Current.Response.Write("<script type='text/javascript'>alert('读取模板文件错误!')</script>" + exp.Message); HttpContext.Current.Response.End(); sr.Close(); } // 替换内容 str = str.Replace("$txt$", strtxt); str = str.Replace("$pic$", strpic); str = str.Replace("$lnk$", strlnk); try sw = new StreamWriter(jsfile, false, code); sw.Write(str); sw.Flush(); catch (Exception ex) HttpContext.Current.Response.Write("<script type='text/javascript'>alert('生成JS文件出错!')</script>" + ex.Message); HttpContext.Current.Response.End(); finally sw.Flush(); sw.Close(); } } //以下是自定义删除原有JS文件函数 protected void deljs(string jsfile) if (File.Exists(jsfile)) File.Delete(jsfile); else Response.Write("<script type='text/javascript'>alert('系统中不存在能产生首页切换图片的文件!')</script>"); } }

JS文件模板 JsTemplete.js

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwgxs.html