<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpLoad.aspx.cs" Inherits="UpLoad" %>
<!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">
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</HEAD>
<body>
<form id="form1" runat="server">
<div align="center">
<h3>多文件上传</h3>
<p>
<input id="HIF" type="file" runat="server" /> </p>
<p>
<asp:ListBox ID="FileList" runat="server" Width="201px"></asp:ListBox> </p>
<p>
<asp:Button ID="AddFile" runat="server" Text="添加文件" OnClick="AddFile_Click" />
<asp:Button ID="RemvFile" runat="server" Text="删除文件" OnClick="RemvFile_Click" />
<input id="Uploads" type="submit" value="上传" runat="server" onserverclick="Uploads_ServerClick" />
</p>
<p>
<asp:Label ID="TipInfo" runat="server"></asp:Label> </p>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Drawing;
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 UpLoad : System.Web.UI.Page
{
static public ArrayList hif = new ArrayList(); // 保存文件列表
public int filesUploaded = 0; // 上传文件的数量
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 将要上传的文件添加到listbox中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void AddFile_Click(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
hif.Add(HIF);
FileList.Items.Add(HIF.PostedFile.FileName);
}
else
{ }
}
/// <summary>
/// 从listbox中删除指定的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void RemvFile_Click(object sender, 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>
protected void Uploads_ServerClick(object sender, EventArgs e)
{
string baseLocation = Server.MapPath("Uploads/"); // 上传路径
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();
}
}
}