在线等!急急急,ASP.NET后台C#代码如何遍历.ASPX页面中的上传控件File元素!

liu_GT 2009-03-17 11:44:46
小弟正在做一个批量上传图片的功能,页面代码如下:
默认页面上有一个上传控件,一个图片框,当点击"添加上传控件"按钮时,页面上会多出一个上传控件和一个图片框,假哪客户一共添加了五个上传控件,那后台代码如何遍历上传控件,并取里面的值,存到数据库??

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BH_Add_Car_Pic.aspx.cs" Inherits="manage_BH_Add_Car_Pic" %>

<!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>

<script language="JavaScript">
var theNum=1;
function addFileControl()
{
theNum=theNum+1;
//var strFocus='';
var str = '<table style="width: 500px"><tr><td style="width: 300px"><input name="file" type="file" style="width: 300px" onfocus="javascript:ShowImage(this.value,document.getElementById(\'img_'+theNum+'\'))" /></td><td style="width: 200px" align="center"><img id="img_'+theNum+'" src="/img/up.jpg" width="100" height="100" /></td></tr></table>'
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
}
</script>
<script type="text/javascript">
function ShowImage(thePath,obj)
{
obj.src=thePath;
}
</script>
</head>
<body>
<div id="content">
<form name="form1" id="form2" runat="server">
<div style="text-align:left">
<div class="bdiv">

<table width="500px" cellpadding="0" cellspacing="0">
<tr>
<td>
添加汽车展示图片
<input id="Button2" type="button" value="添加上传控件" onclick="addFileControl()" /></td>
</tr>
</table>
<P id="FileCollection" style="width:500px;">
<table style="width: 500px">
<tr>
<td style="width: 300px">
<input type="file" name="file" style="width: 300px" onfocus="javascript:ShowImage(this.value,document.getElementById('img_1'))" />
</td>
<td style="width: 200px" align="center">
<img id="img_1" src="/img/up.jpg" width="100" height="100" />
</td>
</tr>
</table>
</P>
<table width="500px" cellpadding="0" cellspacing="0">
<tr>
<td style="width:500px" align="center">
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />

</td>
</tr>
</table>
<br />
<P align="center" style="width:500px;"><asp:label id="strStatus1" runat="server" BorderColor="White" BorderStyle="None" Width="500px"
Font-Size="9pt" Font-Bold="True" Font-Names="宋体"></asp:label></P>
</div>
</div>
</form>
</div>
</body>
</html>


后台页面代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manage_BH_Add_Car_Pic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
//遍历File表单元素
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
System.Web.HttpFileCollection Files = System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
int fileCount;
int filecount = files.Count;
try
{
for (fileCount = 0; fileCount < files.Count; fileCount++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile = files[fileCount];
string fileName, fileExtension;
//取得上传得文件名
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != String.Empty)
{
//取得文件的扩展名
fileExtension = System.IO.Path.GetExtension(fileName);
//上传的文件信息
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
//保存到指定的文件夹
postedFile.SaveAs(Server.MapPath("/Files/Pic/") + fileName);
}
}
strStatus1.Text = strMsg.ToString();
//return true;
}
catch (System.Exception error)
{
strStatus1.Text = error.Message;
//return false;

}
}
}


请达人赐教!在线等!!!!
...全文
584 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
volnet 2009-03-18
  • 打赏
  • 举报
回复
你确定在线等?帮你看看
jlj84237485 2009-03-18
  • 打赏
  • 举报
回复
帮顶一下
hubofly 2009-03-18
  • 打赏
  • 举报
回复
用js 遍历你的input 标签,根据你的标签类型来获取你input标签里面的值 然后判断上传等
liuyeede 2009-03-18
  • 打赏
  • 举报
回复
我可以明确的告诉你,你的设计思路有问题。为什么添加一个文件要用一次上传控件?
最后的结果是:由于上传控件是动态生成的,在最后上传文件时又找不到你的动态生成的控件。同时,写出来的代码结构不好,不优美。

1、用一个FileUpload控件结合一个DataList或者GridView就完全可以做出多文件上传的效果。
2、将FileUpLoad中选择出的文件信息放到一个列表中,并绑定到DataList或者GridView上(可以设计删除等功能)
3、当用户选择玩要上传的文件后,更加列表里的文件信息上传文件(遍历列表,逐个上传文件)。
liu_GT 2009-03-18
  • 打赏
  • 举报
回复
跟据hubofly的提示,我是这么写的:
我又在页面上放了一个文本框,将来改成隐藏的控件
<input runat="server" id="Text1" type="text" />
在JS函数ShowImage中我又加了几句JS代码,如下:
<script type="text/javascript">
function ShowImage(thePath,obj)
{
obj.src=thePath;

//以下新加的
document.getElementById('Text1').value='';
var i=0;
filesup = document.getElementsByName("file");
for (i;i<filesup.length;i++)
{
document.getElementById('Text1').value+=filesup[i].value+";";
}
}
</script>

这样,客户每选一个图片,都会把所有file的值从重写入Text1中.
我在后台添加控件事件中,直接读取Text1的值,再保存.
liu_GT 2009-03-18
  • 打赏
  • 举报
回复
我是在线等.

62,268

社区成员

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

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

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

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