62,025
社区成员
发帖
与我相关
我的任务
分享
protected void UploadButton_Click(object sender, EventArgs e) //上传图片
{
SaveImages();
dlbd();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.ToString() == "del")
{
Session["fnm"] = Session["fnm"].ToString().Replace(e.CommandArgument.ToString() + ",", null);
dlbd();
//删除相应文件
if (Io.Deletefile(Server.MapPath(e.CommandArgument.ToString())))
{
if (int.Parse(Session["Count"].ToString()) > 0)
{
Session["Count"] = int.Parse((Session["Count"]).ToString()) - 1;
}
Func.Alert("删除成功", this);
}
else
{
Func.Alert("删除失败", this);
}
//Response.Write("删除后的个数:" + Session["Count"].ToString());
}
}
public void dlbd()
{
if (Session["fnm"].ToString().Length > 0)
{
DataList1.Visible = true;
DataList1.DataSource = Session["fnm"].ToString().Substring(0, Session["fnm"].ToString().Length - 1).Split(',');
DataList1.DataBind();
return;
}
DataList1.Visible = false;
}
private Boolean SaveImages() //上传
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
//strMsg.Append("上传的文件分别是: <hr color=red>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
if (postedFile.ContentLength > 1048576)
{
strMsg.Append(fileName + "文件超过1M,上传失败!<br><hr>");
}
else
{
if (StringCheck.Imgtill(fileName))
{
string Testimgname = Guid.NewGuid().ToString() + fileExtension;
ViewState["fnm"] = "";
ViewState["fnm"] += "../uploadimg/Supply/" + Login.LogUserid.ToString() + "/" + Testimgname + ",";
if (Session["fnm"].ToString().Split(',').Length > 5)
{
Func.Alert("最多允许上传5张图片", this);
return false;
}
Session["fnm"] += ViewState["fnm"].ToString();
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../uploadimg/Supply/" + Login.LogUserid.ToString()) + "/" + Testimgname);
//strMsg.Append("文件" + fileName + "上传成功<hr>");
}
else
{
strMsg.Append("文件" + fileName + "上传失败,只允许Gif,Jpg格式图片!<hr>");
}
}
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}