我用js在前台页面动态添加多个<input type="file"> 为什么在CS页面只能获取第一个file呢?前台代码如下:
<body>
<form id="form1" method="post" enctype="multipart/form-data" runat="server">
<div>
<table width="319" height="38" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="32">
<label><a>添加文件</a></label>
</td>
<td height="32" id="td1">
<input type="file" name="file0" id="file0" style="z-index=-1" onchange="javascript:onchangeFile()"/>
</td>
</tr>
</table>
</form>
</body>cs页面代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(AJax_CommonFileUpLoad));
}
if (IsPostBack)
{
UpLoad(); }
}
protected void UpLoad()
{
string[] sdr = Request.Files.AllKeys;
foreach (string str in sdr)
{
Response.Write(str+"<br>");
}
HttpFileCollection files = HttpContext.Current.Request.Files;
Response.Write(files.Count);
}js代码如下:
var td=document.getElementById("td1");
number+=1;
td.innerHTML+="<input type=\"file\" id=\"file"+number+"\" onchange='javascript:onchangeFile()'>";
<body>
<form id="form1" method="post" enctype="multipart/form-data" runat="server">
<div>
<table width="319" height="38" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="32">
<label><a>添加文件</a></label>
</td>
<td height="32" id="td1">
<input type="file" name="file0" id="file0" style="z-index=-1" onchange="javascript:onchangeFile()"/>
</td>
</tr>
</table>
</form>
</body>cs页面代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(AJax_CommonFileUpLoad));
}
if (IsPostBack)
{
UpLoad(); }
}
protected void UpLoad()
{
string[] sdr = Request.Files.AllKeys;
foreach (string str in sdr)
{
Response.Write(str+"<br>");
}
HttpFileCollection files = HttpContext.Current.Request.Files;
Response.Write(files.Count);
}js代码如下:
var td=document.getElementById("td1");
number+=1;
td.innerHTML+="<input type=\"file\" id=\"file"+number+"\" onchange='javascript:onchangeFile()'>";
Response.Write(str+"<br>"); 总是file0 也就是不是动态添加的那个file
Reqeust.PostFiles[i].SaveAs()
}拼写纯属胡写 基本就这个意思 肯定能实现的
添加的时候要给每个file添加不同的name 否则可能只有1个