多文件上传 C#如何实现多文件上传,求代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/u/20100427/10/c50aff62-1581-47f9-a19c-ea1293936a1f.html OpenFileDialog记录选取的文件名,记录到数组遍历数组上传即可和上传单文件一样,只是把逻辑加了个循环而已 不知道你要的是webform的还是winform的。先给你一个webform的参考:<%@ Page language="c#" Codebehind="Attachme.aspx.cs" AutoEventWireup="false" Inherits="UploadFile.Attachme" %><HTML> <HEAD> <title>多文件上传</title> <LINK href="StyleSheet.css" rel="stylesheet"> </HEAD> <body> <form id="attachme" method="post" encType="multipart/form-data" runat="server"> <INPUT class="bluebutton" id="FindFile" type="file" size="26" runat="server" NAME="FindFile"> <BR> <asp:listbox id="FileList" runat="server" CssClass="txtbox" Height="100px" Width="274px" SelectionMode="Multiple"></asp:listbox><BR> <asp:button id="AddFile" runat="server" CssClass="bluebutton" Height="23px" Width="72px" Text="添加文件"></asp:button> <asp:button id="RemvFile" runat="server" CssClass="bluebutton" Height="23px" Width="72px" Text="删除文件"></asp:button> <INPUT class="bluebutton" id="Upload" type="submit" value="上传" runat="server" onserverclick="Upload_ServerClick" NAME="Upload"> </form> <asp:label id="TipInfo" runat="server" Height="25px" Width="249px"></asp:label> </body></HTML>---Attachme.aspx.csusing System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace UploadFile{ /// <summary> /// 多文件上传 /// </summary> public class Attachme : System.Web.UI.Page { protected System.Web.UI.WebControls.Button AddFile; protected System.Web.UI.WebControls.Button RemvFile; protected System.Web.UI.HtmlControls.HtmlInputFile FindFile; protected System.Web.UI.HtmlControls.HtmlInputButton Upload; protected System.Web.UI.HtmlControls.HtmlGenericControl txtOutput; protected System.Web.UI.WebControls.ListBox FileList; protected System.Web.UI.WebControls.Label TipInfo; static public ArrayList hif = new ArrayList(); // 保存文件列表 public int filesUploaded = 0; // 上传文件的数量 private void Page_Load(object sender, System.EventArgs e) { } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.AddFile.Click += new System.EventHandler(this.AddFile_Click); this.RemvFile.Click += new System.EventHandler(this.RemvFile_Click); this.Upload.ServerClick += new System.EventHandler(this.Upload_ServerClick); this.Load += new System.EventHandler(this.Page_Load); } #endregion /// <summary> /// 将要上传的文件添加到listbox中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddFile_Click(object sender, System.EventArgs e) { if (Page.IsPostBack == true) { hif.Add(FindFile); FileList.Items.Add(FindFile.PostedFile.FileName); } else {} } /// <summary> /// 从listbox中删除指定的文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RemvFile_Click(object sender, System.EventArgs e) { if(FileList.SelectedIndex == -1) { TipInfo.Text = "错误 - 必须指定要删除的文件."; return; } else if(FileList.Items.Count != 0) { hif.RemoveAt(FileList.SelectedIndex); FileList.Items.Remove(FileList.SelectedItem.Text); TipInfo.Text = ""; } } /// <summary> /// 循环上传listbox中的文件到指定的文件夹下 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Upload_ServerClick(object sender, System.EventArgs e) { string baseLocation = Server.MapPath("UploadFiles/"); // 上传路径 string status = ""; // 上传成功后显示的文件列表 if((FileList.Items.Count == 0) && (filesUploaded == 0)) { TipInfo.Text = "错误 - 必须指定要上传的文件."; return; } else { foreach(System.Web.UI.HtmlControls.HtmlInputFile HIF in hif) { try { string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName); HIF.PostedFile.SaveAs(baseLocation + fn); filesUploaded++; status += fn + "<br>"; } catch(Exception err) { TipInfo.Text = "上传错误 " + baseLocation + "<br>" + err.ToString(); } } if(filesUploaded == hif.Count) { TipInfo.Text = "共上传了 " + filesUploaded + " 个文件。 <br>" + status; } hif.Clear(); FileList.Items.Clear(); } } }} 这个Label点击事件,为什么没对呢 VS 2008 的 WinForms 的工程文件能否让 VS 2005 完美支持? linq to entity 连接Mysql ,本地正常,服务器上“未将对象引用设置到对象的实例。” 未将对象引用设置到对象的实例? winrar命令行 C#中事件 请问web的CrystalReportViewer 控件怎么没有打印按键的? 请教:如何在.net中实现同Php中 crypt()函数 和 md5()函数相同的加密算法 时间问题 求教类似数学建模,带未知数的四则运算 WPF多线程求助 drawstring在winxp与win7下效果不一样
遍历数组上传即可和上传单文件一样,只是把逻辑加了个循环而已
<%@ Page language="c#" Codebehind="Attachme.aspx.cs" AutoEventWireup="false" Inherits="UploadFile.Attachme" %>
<HTML>
<HEAD>
<title>多文件上传</title>
<LINK href="StyleSheet.css" rel="stylesheet">
</HEAD>
<body>
<form id="attachme" method="post" encType="multipart/form-data" runat="server">
<INPUT class="bluebutton" id="FindFile" type="file" size="26" runat="server" NAME="FindFile">
<BR>
<asp:listbox id="FileList" runat="server" CssClass="txtbox" Height="100px" Width="274px" SelectionMode="Multiple"></asp:listbox><BR>
<asp:button id="AddFile" runat="server" CssClass="bluebutton" Height="23px" Width="72px" Text="添加文件"></asp:button>
<asp:button id="RemvFile" runat="server" CssClass="bluebutton" Height="23px" Width="72px" Text="删除文件"></asp:button>
<INPUT class="bluebutton" id="Upload" type="submit" value="上传" runat="server" onserverclick="Upload_ServerClick"
NAME="Upload">
</form>
<asp:label id="TipInfo" runat="server" Height="25px" Width="249px"></asp:label>
</body>
</HTML>---Attachme.aspx.csusing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace UploadFile
{
/// <summary>
/// 多文件上传
/// </summary>
public class Attachme : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button AddFile;
protected System.Web.UI.WebControls.Button RemvFile;
protected System.Web.UI.HtmlControls.HtmlInputFile FindFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Upload;
protected System.Web.UI.HtmlControls.HtmlGenericControl txtOutput;
protected System.Web.UI.WebControls.ListBox FileList;
protected System.Web.UI.WebControls.Label TipInfo; static public ArrayList hif = new ArrayList(); // 保存文件列表
public int filesUploaded = 0; // 上传文件的数量
private void Page_Load(object sender, System.EventArgs e)
{
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.AddFile.Click += new System.EventHandler(this.AddFile_Click);
this.RemvFile.Click += new System.EventHandler(this.RemvFile_Click);
this.Upload.ServerClick += new System.EventHandler(this.Upload_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion /// <summary>
/// 将要上传的文件添加到listbox中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddFile_Click(object sender, System.EventArgs e)
{
if (Page.IsPostBack == true)
{
hif.Add(FindFile);
FileList.Items.Add(FindFile.PostedFile.FileName);
}
else
{}
} /// <summary>
/// 从listbox中删除指定的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RemvFile_Click(object sender, System.EventArgs e)
{
if(FileList.SelectedIndex == -1)
{
TipInfo.Text = "错误 - 必须指定要删除的文件.";
return;
}
else if(FileList.Items.Count != 0)
{
hif.RemoveAt(FileList.SelectedIndex);
FileList.Items.Remove(FileList.SelectedItem.Text);
TipInfo.Text = "";
}
} /// <summary>
/// 循环上传listbox中的文件到指定的文件夹下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Upload_ServerClick(object sender, System.EventArgs e)
{
string baseLocation = Server.MapPath("UploadFiles/"); // 上传路径
string status = ""; // 上传成功后显示的文件列表
if((FileList.Items.Count == 0) && (filesUploaded == 0))
{
TipInfo.Text = "错误 - 必须指定要上传的文件.";
return;
}
else
{
foreach(System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
{
try
{
string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
HIF.PostedFile.SaveAs(baseLocation + fn);
filesUploaded++;
status += fn + "<br>";
}
catch(Exception err)
{
TipInfo.Text = "上传错误 " + baseLocation
+ "<br>" + err.ToString();
}
} if(filesUploaded == hif.Count)
{
TipInfo.Text = "共上传了 " + filesUploaded + " 个文件。 <br>" + status;
}
hif.Clear();
FileList.Items.Clear();
}
}
}
}