温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Web图片管理系统代码
当前文件:
PictureManager/Upload.aspx.cs,打开代码结构图
PictureManager/Upload.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
12
public partial class Upload : System.Web.UI.Page 13
{ 14
SQLHelper helper = new SQLHelper(); 15
protected void Page_Load(object sender, EventArgs e) 16
{ 17
if (!IsPostBack) 18
{ 19
if (Request.Params["ImgID"] != null) 20
{ 21
btnUploadPic.Enabled = true; 22
ViewState["ImgID"] = Request.Params["ImgID"].ToString(); 23
} 24
else 25
{ 26
btnUploadPic.Enabled = false; 27
ViewState["ImgID"] = ""; 28
} 29
} 30
31
} 32
protected void btnUploadPic_Click(object sender, EventArgs e) 33
{ 34
if (!tbxPicPath.HasFile) 35
{ 36
Response.Write("<script>window.alert('请指定上传路径!')</script>"); 37
return; 38
} 39
else if (tbxPicPath.PostedFile.ContentLength == 0) 40
{ 41
Response.Write("<script>window.alert('文件的内容不能为空!')</script>"); 42
return; 43
} 44
45
// 获取上传图片的文件名 46
String fileName = tbxPicPath.PostedFile.FileName.Substring(tbxPicPath.PostedFile.FileName.LastIndexOf("\\")); 47
48
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName)) 49
{ 50
Response.Write("<script>window.alert('该文件名在服务器中已存在,请更改文件名!')</script>"); 51
} 52
else 53
{ 54
try 55
{ 56
// 上传文件 57
tbxPicPath.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName); 58
// 添加数据库信息 59
helper.addNode(tbxPicName.Text.Trim(), Request.ApplicationPath + "\\Pictures" + fileName, "0", ViewState["ImgID"].ToString()); 60
// 显示上载成功的信息 61
Response.Write("<script>window.alert('文件上传成功!')</script>"); 62
} 63
catch (Exception ex) 64
{ 65
Response.Write("<script>window.alert('由于网络原因,上载文件错误 " + ex.Message + "')</script>"); 66
} 67
68
tbxPicName.Text = ""; 69
} 70
} 71
protected void btnReturn_Click(object sender, EventArgs e) 72
{ 73
Response.Redirect("~/List.aspx"); 74
} 75
} 76




SQLHelper helper 
}
}