📄 upload.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;
public partial class Upload : System.Web.UI.Page
{
SQLHelper helper = new SQLHelper();
// 画面装载
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Params["ImgID"] != null)
{
btnUpload.Enabled = true;
ViewState["ImgID"] = Request.Params["ImgID"].ToString();
}
else
{
btnUpload.Enabled = false;
ViewState["ImgID"] = "";
}
}
}
// 上传图片
protected void btnUpload_Click(object sender, EventArgs e)
{
if (tbxPath.Value.ToString() == "")
{
Response.Write("<script>window.alert('请指定上传路径!')</script>");
}
else if (tbxPath.PostedFile.ContentLength == 0)
{
Response.Write("<script>window.alert('文件的内容不能为空!')</script>");
}
// 获取上传图片的文件名
String fileName = tbxPath.PostedFile.FileName.Substring(tbxPath.PostedFile.FileName.LastIndexOf("\\"));
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName))
{
Response.Write("<script>window.alert('该文件名在服务器中已存在,请更改文件名!')</script>");
}
else
{
try
{
// 上传文件
tbxPath.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName);
// 添加数据库信息
helper.AddItem(tbxName.Text.Trim(), Request.ApplicationPath + "\\Pictures" + fileName, "0", ViewState["ImgID"].ToString());
// 显示上载成功的信息
Response.Write("<script>window.alert('文件上传成功!')</script>");
}
catch (Exception ex)
{
Response.Write("<script>window.alert('由于网络原因,上载文件错误 " + ex.Message + "')</script>");
}
tbxName.Text = "";
}
}
// 返回图片列表
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("~/List.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -