📄 addthemes.aspx.cs
字号:
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.IO;
using lalablog;
public partial class admin_addThemes : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] == null)
Response.Redirect("adminLogin.aspx");
}
//第二步,上传图片
protected void Button2_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/app_themes/" + ViewState["newTheme"].ToString() + "/");
Label2.Text = path;
if (Directory.Exists(path) && FileUpload1.HasFile)
upImage();
// else
// Label2.Text = "未能上传文件";
}
private void upImage()
{
Boolean fileOK = false;
string path = Server.MapPath("~/app_themes/" + ViewState["newTheme"].ToString() + "/");
string filename;
//检验是否指定后缀
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
fileOK = true;
}
int fileSize = FileUpload1.PostedFile.ContentLength;
//利用postedFile.ContentLength来判断文件长度,检查是否符合文件大小,250000就是250k字节
if (fileOK && fileSize < 1000000)
fileOK = true;
else
fileOK = false;
}
if (fileOK)
{
try
{
filename = path + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(filename);
Label2.Text = "成功上传图片至" + filename;
}
catch (Exception ex)
{
myfunc m1 = new myfunc(ex.Message);
Response.Write(m1.JavaMsg());
}
}
else
{
myfunc m1 = new myfunc("无法上传的文件类型或文件大小超过允许值");
Response.Write(m1.JavaMsg());
}
}
private void loadcss()
{
string path = Server.MapPath("~/app_themes/" + ViewState["newTheme"].ToString() + "/");
string blogcss = path + "\\blog.css";
try
{
tbThemeCss.Text = File.ReadAllText( blogcss);
Label4.Text = "成功读取css文件于" + blogcss;
}
catch (Exception ex)
{
Label4.Text = ex.Message;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string newThemePath;
newThemePath = Server.MapPath("~/app_themes/" + Server.HtmlEncode(tbNewTheme.Text));
if (Directory.Exists(newThemePath))
{
Label1.Text = "主题已存在,如果您不更改名字,将对已存在的主题进行编辑,当前编辑的主题是!"+tbNewTheme .Text ;
ViewState["newTheme"] = tbNewTheme.Text;
loadcss();
}
else
{
try
{
//新增主题并写入数据库
Directory.CreateDirectory(newThemePath);
Label1.Text = "创建新主题成功,当前编辑的主题是" + tbNewTheme.Text;
ViewState["newTheme"] = tbNewTheme.Text;
writeToDb();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
}
private void writeToDb()
{
string name = ViewState["newTheme"].ToString();
string sql = "insert into theme(name,about) values('" + name + "','" + name + "')";
openDb o1 = new openDb(sql);
o1.update();
}
//编辑css文件
protected void Button3_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/app_themes/" + ViewState["newTheme"].ToString() + "/");
string blogcss = path + "\\blog.css";
try
{
File.WriteAllText(blogcss, Server.HtmlEncode(tbThemeCss.Text));
Label4.Text = "成功写入css文件于" + blogcss;
}
catch (Exception ex)
{
Label4.Text = ex.Message;
}
}
//上传skin文件
protected void Button4_Click(object sender, EventArgs e)
{
Boolean fileOK = false;
string path = Server.MapPath("~/app_themes/" + ViewState["newTheme"].ToString() + "/");
string filename = path + "\\blog.skin"; ;
//检验是否指定后缀
if (FileUpload2.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
if (fileExtension ==".skin")
fileOK = true;
int fileSize = FileUpload2.PostedFile.ContentLength;
//利用postedFile.ContentLength来判断文件长度,检查是否符合文件大小,250000就是250k字节
if (fileOK && fileSize < 10000)
fileOK = true;
else
fileOK = false;
}
if (fileOK)
{
try
{
FileUpload2.PostedFile.SaveAs(filename);
Label3.Text = "成功上传skin文件至" + filename;
}
catch (Exception ex)
{
myfunc m1 = new myfunc(ex.Message);
Response.Write(m1.JavaMsg());
}
}
else
{
myfunc m1 = new myfunc("无法上传的文件类型或文件大小超过允许值");
Response.Write(m1.JavaMsg());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -