关于js动态添加上传文件,怎么获取数据的问题?
我用下面这个aspx页面,动态添加上传文件的file框。可是,后台如何获取到上传的文件内容呢?因为文件上传框是动态生成的。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TaskManager.list.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style5
{
width: 128px;
}
.style2
{
font-size: xx-large;
font-weight: bold;
font-family: 宋体, Arial, Helvetica, sans-serif;
}
.style3
{
width: 94px;
}
.style1
{
width: 382px;
}
.style6
{
text-align: center;
width: 128px;
}
.style4
{
text-align: center;
}
</style>
<script language="javascript" type="text/javascript">
// <![CDATA[
function Button3_onclick() {
var rowid = document.getElementById("hidRownum").value;
newRow = PLList.insertRow(PLList.rows.length);
newRow.id = "tdt";
newRow.bgColor = "#e0e0e0";
newRow.className = "tableData";
c1 = newRow.insertCell(0);
c1.id = "delItem";
c1.className = "scheduleButtonVisible";
c1.innerHTML = "<input id=filelist"+rowid+" type=file />";
c2 = newRow.insertCell(1);
c2.innerHTML = "说明:";
c3 = newRow.insertCell(2);
c3.innerHTML = "<input type=text size=10 maxlength=10 id=txt" + rowid + ">";
document.getElementById("hidRownum").value = parseInt(document.getElementById("hidRownum").value) + 1;
}
//删除最后1行
function DeleteRow(tableId) {
var objTable = document.getElementById(tableId);
if (objTable.rows.length == 1) {
alert("对不起,你不能删除表格头!!!");
return;
}
if (confirm("确定删除?"))
{ objTable.deleteRow(); }
}
// ]]>
</script>
</head>
<body>
<form id="form2" runat="server" enctype="multipart/form-data">
<table style="width:100%;">
<tr>
<td class="style5">
</td>
<td colspan="3">
<span class="style2" lang="zh-cn">新增:<asp:HiddenField ID="hidRownum"
runat="server" Value="0" />
</span></td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style3">
<span lang="zh-cn">图片:</span></td>
<td class="style1">
<span lang="zh-cn"> </span><input id="Button3" type="button"
value="新增上传" onclick="return Button3_onclick()" /></td>
<td>
</td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style3">
</td>
<td colspan="2" id="listfile">
<table><tr><td>
<table id=PLList><tr><td>aa</td><td>bb</td><td>cc</td></tr></table>
</td></tr></table>
<input type="button" onClick="DeleteRow('PLList')" value="删除一行">
</td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style3">
</td>
<td class="style1">
<span lang="zh-cn"><asp:Label ID="Label1" runat="server" Text="请选择需要上传的文件!"></asp:Label>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style6">
</td>
<td class="style4" colspan="2">
<asp:Button ID="Button1" runat="server" Text="确 定" onclick="Button1_Click" />
<span lang="zh-cn"> </span>
<asp:Button ID="Button2" runat="server" Text="取 消" />
</td>
<td class="style4">
</td>
</tr>
</table>
</form>
</body>
</html>
用这种方法测试了几次都不行。FindControl为空。
protected void Button1_Click(object sender, EventArgs e)
{
int introw = int.Parse(this.hidRownum.Value);
string strpath = "d:\\";
for (int i = 0; i < introw; i++)
{
string strid = "filelist" + i.ToString();
System.Web.UI.HtmlControls.HtmlInputFile htmlfile = (System.Web.UI.HtmlControls.HtmlInputFile)Page.FindControl(strid);
HttpPostedFile httpFile = htmlfile.PostedFile;
strpath += "\\" + i.ToString();
if (httpFile.ContentLength != 0)
{
if (!Directory.Exists(strpath))
Directory.CreateDirectory(strpath);
string strFileName = Path.GetFileName(httpFile.FileName);
httpFile.SaveAs(strpath + "\\" + strFileName);
}
}
}