WebForm中动态批量文件上传

在WebForm中动态添加FileUpload控件和实现批量文件上传。

Web.config配置:

 
  
< appSettings >
< add key = " FileUpLoadPath " value = " ~/Content/Upload/ " />
< add key = " FileTypeLimit " value = " .jpg,.gif " />
< add key = " FileSizeLimit " value = " 1024000 " />
</ appSettings >

前台代码:

 
  
< table class = " gbtext " id = " tb_UploadedFiles " style = " BORDER-COLLAPSE: collapse " borderColor = " #93bee2 "
cellSpacing
= " 0 " cellPadding = " 0 " width = " 100% " align = " center " border = " 1 " runat = " server " >
< tr >
< td style = " WIDTH: 94px " >< font face = " 宋体 " > 上传数量: </ font ></ td >
< td >
< asp:DropDownList ID = " ddl_USL " runat = " server " AutoPostBack = " True "
onselectedindexchanged
= " ddl_USL_SelectIndex " >
< asp:ListItem Value = " 0 " Text = " 请选择 " ></ asp:ListItem >
< asp:ListItem Value = " 1 " Text = " 1个 " ></ asp:ListItem >
< asp:ListItem Value = " 2 " Text = " 2个 " ></ asp:ListItem >
< asp:ListItem Value = " 3 " Text = " 3个 " ></ asp:ListItem >
< asp:ListItem Value = " 4 " Text = " 4个 " ></ asp:ListItem >
< asp:ListItem Value = " 5 " Text = " 5个 " ></ asp:ListItem >
< asp:ListItem Value = " 6 " Text = " 6个 " ></ asp:ListItem >
< asp:ListItem Value = " 7 " Text = " 7个 " ></ asp:ListItem >
< asp:ListItem Value = " 8 " Text = " 8个 " ></ asp:ListItem >
< asp:ListItem Value = " 9 " Text = " 9个 " ></ asp:ListItem >
< asp:ListItem Value = " 10 " Text = " 10个 " ></ asp:ListItem >
</ asp:DropDownList >
</ td >

</ tr >
< tr >
< td style = " WIDTH: 94px " >< font face = " 宋体 " ></ font ></ td >
< td >
< asp:Panel ID = " pl_UF " runat = " server " >

</ asp:Panel >
</ td >
</ tr >
< tr >
< td style = " WIDTH: 94px " >< font face = " 宋体 " ></ font ></ td >
< td >< font face = " 宋体 " >
< input type = " submit " value = " 上传 " class = " ButtonCss " />
</ font ></ td >
</ tr >
</ table >

后台代码:

 
  
protected void Page_Load( object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Request.Files.AllKeys.Any())
{
var apath
= ConfigurationManager.AppSettings[ " FileUpLoadPath " ];
var fileSize
= Convert.ToInt32(ConfigurationManager.AppSettings[ " FileSizeLimit " ]);
string path = Server.MapPath(apath);
try
{
if ( ! Directory.Exists(path))
Directory.CreateDirectory(path);
foreach ( string f in Request.Files.AllKeys)
{
if (Request.Files[f].ContentLength == 0 ) continue ;
if (Request.Files[f].ContentLength > fileSize) continue ; // 图片大于配置文件大小不上传

string filename = Path.GetFileName(Request.Files[f].FileName);
Request.Files[f].SaveAs(Path.Combine(path, filename));
pl_UF.Controls.Add(
new Label { Text = filename + " 上传成功! " });
pl_UF.Controls.Add(
new Literal { Text = " <br /> " });
}
}
catch (Exception ex)
{
Response.Redirect(
" ~/Error?error= " + ex.Message);
}
}
}
}

protected void ddl_USL_SelectIndex( object sender, EventArgs e)
{
int sl = Convert.ToInt32(ddl_USL.Items[ddl_USL.SelectedIndex].Value);
if (sl != 0 )
{
for ( int x = 0 ; x < sl; x ++ )
{
pl_UF.Controls.Add(
new FileUpload { ID = " FU " + x.ToString() });
pl_UF.Controls.Add(
new Literal { Text = " <br /> " });
}
}
}

实现效果:

 2011040616131154.jpg

 

2011040616132537.jpg

 

2011040616134087.jpg

转载于:https://www.cnblogs.com/yeaszi/archive/2011/04/06/2006931.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值