这是一个上传按钮的事件,如何在其中的Literal1,Literal2,Literal3中获得
<input id='delete' type='button' runat='server' value='删除' />按钮,并实现删除
FileUpload1上传的附件????(提示:点上传时已经调用FileUpload1.PostedFile.SaveAs()方法上传到服务器了,所以要删除的话不光要删除页面的,还要删除upload文件夹下已上传的附件)
protected void 上传_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName.Trim() != "")
{
//上传文件
string fileName = FileUpload1.FileName;//获取文件名
string type = (fileName.Substring(fileName.LastIndexOf(".") + 1)).ToLower();//获取上传文件的类型
//判断文件类型
if ((type == "jpg" || type == "jpeg" || type == "gif" || type == "png" || type == "swf" || type == "rar" || type == "zip") && FileUpload1.PostedFile.ContentLength < 2 * 1024 * 1024)
{
//计数器
ViewState["count"] = ViewState["count"] == null ? "0" : ViewState["count"];
if (ViewState["count"].ToString() == "5")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('一次最多上传5个附件!')</script>");
return;
}
string path = Server.MapPath("upload");
fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
this.FileUpload1.PostedFile.SaveAs(path + "\\" + fileName);
if (type == "jpg" || type == "jpeg" || type == "gif" || type == "png")
{
ViewState["count"] = int.Parse(ViewState["count"].ToString()) + 1;
if ((int.Parse(ViewState["count"].ToString()) % 3) == 0)
{
Literal1.Text += "<br/>";
}
Literal1.Text += "<a href='#' onclick=\"window.showModalDialog('Img.htm?img=upload/" + fileName + "','','dialogWidth=75;dialogHeight=50;help=0;status=0;resizable=1')\"><img src='upload/" + fileName + "' width='25px' height='25' border=0 />" + fileName + "</a> <input id='delete' type='button' runat='server' value='删除' />";
Hidden1.Value += "upload/" + fileName + ",";
}
if (type == "rar" || type == "zip")
{
ViewState["count"] = int.Parse(ViewState["count"].ToString()) + 1;
if ((int.Parse(ViewState["count"].ToString()) % 3) == 0)
{
Literal2.Text += "<br/>";
}
Literal2.Text += "<img src='images/yasuo.jpg' width='25px' height='25' border=0 />" + fileName + " <input id='delete' type='button' runat='server' value='删除' />";
Hidden1.Value += "upload/" + fileName + ",";
}
if (type == "swf")
{
ViewState["count"] = int.Parse(ViewState["count"].ToString()) + 1;
if ((int.Parse(ViewState["count"].ToString()) % 3) == 0)
{
Literal3.Text += "<br/>";
}
Literal3.Text += "<img src='images/flash.jpg' width='25px' height='25' border=0 />" + fileName + " <input id='delete' type='button' runat='server' value='删除' />";
Hidden1.Value += "upload/" + fileName + ",";
}
}//文件类型if
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件类型不符!!')</script>");
}
}//文件是否为空if
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择文件!!')</script>");
}
}
<input id='delete' type='button' runat='server' value='删除' />按钮,并实现删除
FileUpload1上传的附件????(提示:点上传时已经调用FileUpload1.PostedFile.SaveAs()方法上传到服务器了,所以要删除的话不光要删除页面的,还要删除upload文件夹下已上传的附件)
protected void 上传_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName.Trim() != "")
{
//上传文件
string fileName = FileUpload1.FileName;//获取文件名
string type = (fileName.Substring(fileName.LastIndexOf(".") + 1)).ToLower();//获取上传文件的类型
//判断文件类型
if ((type == "jpg" || type == "jpeg" || type == "gif" || type == "png" || type == "swf" || type == "rar" || type == "zip") && FileUpload1.PostedFile.ContentLength < 2 * 1024 * 1024)
{
//计数器
ViewState["count"] = ViewState["count"] == null ? "0" : ViewState["count"];
if (ViewState["count"].ToString() == "5")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('一次最多上传5个附件!')</script>");
return;
}
string path = Server.MapPath("upload");
fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
this.FileUpload1.PostedFile.SaveAs(path + "\\" + fileName);
if (type == "jpg" || type == "jpeg" || type == "gif" || type == "png")
{
ViewState["count"] = int.Parse(ViewState["count"].ToString()) + 1;
if ((int.Parse(ViewState["count"].ToString()) % 3) == 0)
{
Literal1.Text += "<br/>";
}
Literal1.Text += "<a href='#' onclick=\"window.showModalDialog('Img.htm?img=upload/" + fileName + "','','dialogWidth=75;dialogHeight=50;help=0;status=0;resizable=1')\"><img src='upload/" + fileName + "' width='25px' height='25' border=0 />" + fileName + "</a> <input id='delete' type='button' runat='server' value='删除' />";
Hidden1.Value += "upload/" + fileName + ",";
}
if (type == "rar" || type == "zip")
{
ViewState["count"] = int.Parse(ViewState["count"].ToString()) + 1;
if ((int.Parse(ViewState["count"].ToString()) % 3) == 0)
{
Literal2.Text += "<br/>";
}
Literal2.Text += "<img src='images/yasuo.jpg' width='25px' height='25' border=0 />" + fileName + " <input id='delete' type='button' runat='server' value='删除' />";
Hidden1.Value += "upload/" + fileName + ",";
}
if (type == "swf")
{
ViewState["count"] = int.Parse(ViewState["count"].ToString()) + 1;
if ((int.Parse(ViewState["count"].ToString()) % 3) == 0)
{
Literal3.Text += "<br/>";
}
Literal3.Text += "<img src='images/flash.jpg' width='25px' height='25' border=0 />" + fileName + " <input id='delete' type='button' runat='server' value='删除' />";
Hidden1.Value += "upload/" + fileName + ",";
}
}//文件类型if
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件类型不符!!')</script>");
}
}//文件是否为空if
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择文件!!')</script>");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货