HttpFileCollection 多文件上传的实现以及需要注意的事项

.aspx 页面中的javascript代码


function addFile() 

      var str = '<br /><INPUT type="file" size="50" runat="server" NAME="File">' ;
      document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);

}

 

.aspx 页面中的HTML代码


<P id="MyFile">

   <INPUT type="file" runat="server" size="50" NAME="File" />

</P>
<input type="button" value="增加文件控件" οnclick="addFile()"  />

 

.aspx.cs 中的部分代码


HttpFileCollection uploadFileList= HttpContext.Current.Request.Files;

for (int i = 0; i < uploadFileList.Count; i++)
{
   if (uploadFileList[i].FileName!="")
   {

      string str=uploadFileList[i].FileName;

      string newfilename="test";
      string newfilepath = Server.MapPath("") + "//" + newfilename + str.Substring(str.LastIndexOf("."));
      uploadFileList[i].SaveAs(newfilepath);
    }
}

 

需要注意的是 :

1)需要在 file 控件中加入  runat="server",否则不能被HttpContext.Current.Request.Files取到

2)若出现"Cannot access a closed file" 错误信息,则需要在web.config中配置如下信息:

<system.web>

  <httpRuntime maxRequestLength="819200" requestLengthDiskThreshold="819200" />

</system.web>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值