首页 新闻 会员 周边 捐助

.net使用FileUpload批量上传文件(是先将fileImg添加到Listbox中,最后从listBox中上传到服务器上)各位大侠帮忙看看呗

0
悬赏园豆:10 [待解决问题]

<form id="form1" runat="server">
&nbsp;<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" />&nbsp;
<asp:Button ID="btnUpLoad" runat="server" Text="上传" onclick="btnUpLoad_Click" />&nbsp;
<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();
}

wangling_310的主页 wangling_310 | 初学一级 | 园豆:192
提问于:2014-11-04 15:18
< >
分享
所有回答(1)
0

foreach (System.Web.UI.WebControls.FileUpload item in list)//不能这样取,还有没有别的办法

foreach(var item in list.items)是这样写好吗...

再者这种方式不对,可以在list.item中添加的是文件的路径要不要保存控件对象

Set sail | 园豆:540 (小虾三级) | 2014-11-06 16:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册