关于js动态添加上传文件,怎么获取数据的问题?

casear_huang 2010-11-16 02:36:09
我用下面这个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);
}
}
}
...全文
329 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyerwing 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 telankes2000 的回复:]
C# code
System.Web.HttpFileCollection files = Request.Files;
[/Quote]
FILEUPLOAD
就知道了
casear_huang 2010-11-17
  • 打赏
  • 举报
回复
经过我的测试,终于发现原因了,
c1.innerHTML = "<input id=filelist"+rowid+" type=file />";
脚本这里,要用name,不能用id,写成这样就ok了
c1.innerHTML = "<input name=filelist type=file />";
我只相信汗水 2010-11-17
  • 打赏
  • 举报
回复
这样试下呢,我没时间去调试,你调试看看

for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i] != null)
{
HttpPostedFile CollFile = Request.Files[i];
if (CollFile.FileName.Trim() != "")
{
CollFile.SaveAs("你的文件名");
}
}
}
Zhang_Think 2010-11-17
  • 打赏
  • 举报
回复
.net 里,貌似在页面初始化时至少要有一个 file 控件的 runat="server" 才可以
casear_huang 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chen_ya_ping 的回复:]
引用 3 楼 casear_huang 的回复:

貌似不行啊,HttpPostedFile hpf = files[0];
提示索引超出范围

那你的那个控件就不要动态的创建了,先hide,然后show,就ok了啊。
[/Quote]
我们现在是探讨这个方法是否可行。jsp里面是没有任何问题的,我不相信.net里面不行。
casear_huang 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hch126163 的回复:]
HttpPostedFile file = Request.Files[0];
file.SaveAs(directoryAndName); // 保存到物理路径
[/Quote]

这个是一样的,索引超出范围
hch126163 2010-11-17
  • 打赏
  • 举报
回复

HttpPostedFile file = Request.Files[0];
file.SaveAs(directoryAndName); // 保存到物理路径
chen_ya_ping 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 casear_huang 的回复:]

貌似不行啊,HttpPostedFile hpf = files[0];
提示索引超出范围
[/Quote]
那你的那个控件就不要动态的创建了,先hide,然后show,就ok了啊。
casear_huang 2010-11-17
  • 打赏
  • 举报
回复
貌似不行啊,HttpPostedFile hpf = files[0];
提示索引超出范围
HolyPlace 2010-11-16
  • 打赏
  • 举报
回复

HttpFileCollection files = Request.Files;
HttpPostedFile hpf = files[0];
byte[] image = new byte[hpf.InputStream.Length];
int len = (int)hpf.InputStream.Length;
hpf.InputStream.Read(image, 0, len);
hpf.InputStream.Close();

大致代码
telankes2000 2010-11-16
  • 打赏
  • 举报
回复

System.Web.HttpFileCollection files = Request.Files;

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧