在线等!急急急,ASP.NET后台C#代码如何遍历.ASPX页面中的上传控件File元素!
小弟正在做一个批量上传图片的功能,页面代码如下:
默认页面上有一个上传控件,一个图片框,当点击"添加上传控件"按钮时,页面上会多出一个上传控件和一个图片框,假哪客户一共添加了五个上传控件,那后台代码如何遍历上传控件,并取里面的值,存到数据库??
<%@ 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;
}
}
}
请达人赐教!在线等!!!!