<form id="form1" runat="server">
<asp:FileUpload ID="FileImg" runat="server" /><br />
<asp:ListBox ID="ListFile" runat="server" Height="137px" Width="209px"></asp:ListBox>
<br />
<asp:Button ID="btnAdd" runat="server" Text="添加文件" onclick="btnAdd_Click" />
<asp:Button ID="btnUpLoad" runat="server" Text="上传" onclick="btnUpLoad_Click" />
<asp:Button ID="btnDelete" runat="server" Text="删除" Style="height: 21px"
onclick="btnDelete_Click" /><br />
<asp:Label ID="lblShowMsg" runat="server" Text=""></asp:Label>
</form>
aspx页面
添加到listbox的代码
if (FileImg.HasFile)
{
string FileImgName = Path.GetFileName(FileImg.FileName);
string ExName = Path.GetExtension(FileImgName);
string[] allowName = { ".jpg", ".gif", ".png", ".xls" };
if (allowName.Contains(ExName))
{
for (int i = 0; i < ListFile.Items.Count; i++)
{
//判断listbox中的文件是否重复
if (ListFile.Items[i].Text.Contains(FileImgName))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "<script>alert('该文件在列表中已经存在');</script>", true);
return;
}
}
list.Add(FileImg);
ListFile.Items.Add(FileImgName);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "<script>alert('该文件类型重复');</script>", true);
}
}
从listbox中上传到本地的代码
protected void btnUpLoad_Click(object sender, EventArgs e)
{
string status = "";//状态
if (ListFile.Items.Count == 0 && count == 0)
{
lblShowMsg.Text = "错误 - 必须指定要上传的文件";
return;
}
foreach (System.Web.UI.WebControls.FileUpload item in list)//不能这样取,还有没有别的办法
{
try
{
string FileImgName = Path.GetFileName(item.FileName);
item.PostedFile.SaveAs(Server.MapPath("~/File/" + FileImgName));
count++;
status += FileImgName + "</br>";
}
catch (Exception ex)
{
lblShowMsg.Text = "错误信息——" + Server.MapPath("~/File/") + "</br>" + ex.Message;
}
}
if (count ==list.Count)
{
lblShowMsg.Text = "共有" + count + "文件上传成功</br>" + status;
}
list.Clear();
ListFile.Items.Clear();
}
foreach (System.Web.UI.WebControls.FileUpload item in list)//不能这样取,还有没有别的办法
foreach(var item in list.items)是这样写好吗...
再者这种方式不对,可以在list.item中添加的是文件的路径要不要保存控件对象