asp.net中多文件上传的问题
uploadMoreFile.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upMoreFile.aspx.cs" Inherits="upMoreFile" %>
<!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 type="text/javascript">
function addmoreControl()
{
var str='<input type="file" name="file"><br /><br />';
document.getElementById('fileCollection').insertAdjacentHTML("beforeEnd",str)
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<aspabel ID="Label1" runat="server" Text="多文件上传示例"></aspabel>
<br />
<br />
<div id="fileCollection">
<input id="file" type="file" /><br /><br />
</div>
<br />
<input id="Button1" type="button" value="增加(file)" />
<input id="btUpload" runat="server" type="button" value="button" />
<input id="Button2" type="button" value="重置" />
<br />
<br />
<br />
<aspabel ID="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
-----------------------------------------------------------------------------------
uploadMoreFile.aspx .cs :
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 upMoreFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.btUpload.Value = "上传";
}
protected void btUpload_ServerClick(object sender, EventArgs e)
{
upMorefile();
}
private bool upMorefile()
{
//遍历表单元素
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为: <hr style='color:red;'>");
int fileCount;
fileCount=files.Count;
Response.Write(fileCount.ToString());
try
{
for(int i=0;i<files.Count;i++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile=files;
string fileName,fileExtension;
//取得上传文件名
fileName=System.IO.Path.GetFileName(postedFile.FileName);
if(fileName!=null)
{
//取得文件的扩展名
fileExtension=System.IO.Path.GetExtension(postedFile.FileName);
//上传文件的信息
strMsg.Append("上传的文件类型: "+postedFile.ContentType.ToString()+"<br />");
strMsg.Append("客户端的文件的地址: "+postedFile.FileName+"<br />");
strMsg.Append("上传的文件名: "+fileName+"<br />");
strMsg.Append("上传文件的扩展名: "+fileExtension+"<br />");
//保存到指定的文件夹
postedFile.SaveAs(Server.MapPath("~/uploadfiles/")+fileName);
}
}
lblMessage.Text = strMsg.ToString();
return true;
}
catch(Exception error)
{
lblMessage.Text=error.Message.ToString();
return false;
}
}
}
----------------------------------------------------------------
奇了怪了, 在上传文件的时候如果只有一个file控件,文件不能上传, 断点调试时发现此时的filecount (我用filecount记录file控件集合的个数,见以上代码) 为0;
于上我增加一个file控件,发现可以上传第一个file控件的文件,而此时的filecount为1 , 所以我觉得是程序把第一个file(页面载入时的file控件)省略了. 但又不知道错在哪里了, 麻烦大家帮我看看啊!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upMoreFile.aspx.cs" Inherits="upMoreFile" %>
<!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 type="text/javascript">
function addmoreControl()
{
var str='<input type="file" name="file"><br /><br />';
document.getElementById('fileCollection').insertAdjacentHTML("beforeEnd",str)
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<aspabel ID="Label1" runat="server" Text="多文件上传示例"></aspabel>
<br />
<br />
<div id="fileCollection">
<input id="file" type="file" /><br /><br />
</div>
<br />
<input id="Button1" type="button" value="增加(file)" />
<input id="btUpload" runat="server" type="button" value="button" />
<input id="Button2" type="button" value="重置" />
<br />
<br />
<br />
<aspabel ID="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
-----------------------------------------------------------------------------------
uploadMoreFile.aspx .cs :
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 upMoreFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.btUpload.Value = "上传";
}
protected void btUpload_ServerClick(object sender, EventArgs e)
{
upMorefile();
}
private bool upMorefile()
{
//遍历表单元素
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为: <hr style='color:red;'>");
int fileCount;
fileCount=files.Count;
Response.Write(fileCount.ToString());
try
{
for(int i=0;i<files.Count;i++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile=files;
string fileName,fileExtension;
//取得上传文件名
fileName=System.IO.Path.GetFileName(postedFile.FileName);
if(fileName!=null)
{
//取得文件的扩展名
fileExtension=System.IO.Path.GetExtension(postedFile.FileName);
//上传文件的信息
strMsg.Append("上传的文件类型: "+postedFile.ContentType.ToString()+"<br />");
strMsg.Append("客户端的文件的地址: "+postedFile.FileName+"<br />");
strMsg.Append("上传的文件名: "+fileName+"<br />");
strMsg.Append("上传文件的扩展名: "+fileExtension+"<br />");
//保存到指定的文件夹
postedFile.SaveAs(Server.MapPath("~/uploadfiles/")+fileName);
}
}
lblMessage.Text = strMsg.ToString();
return true;
}
catch(Exception error)
{
lblMessage.Text=error.Message.ToString();
return false;
}
}
}
----------------------------------------------------------------
奇了怪了, 在上传文件的时候如果只有一个file控件,文件不能上传, 断点调试时发现此时的filecount (我用filecount记录file控件集合的个数,见以上代码) 为0;
于上我增加一个file控件,发现可以上传第一个file控件的文件,而此时的filecount为1 , 所以我觉得是程序把第一个file(页面载入时的file控件)省略了. 但又不知道错在哪里了, 麻烦大家帮我看看啊!