温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:51aspx多文件上传示例源码
当前文件:
51aspxUploads/Default.aspx.cs[3K,2009-6-12 11:31:08],打开代码结构图
51aspxUploads/Default.aspx.cs[3K,2009-6-12 11:31:08],打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Data; 6
using System.Drawing; 7
using System.Web; 8
using System.Web.Security; 9
using System.Web.UI; 10
using System.Web.UI.WebControls; 11
using System.Web.UI.WebControls.WebParts; 12
using System.Web.UI.HtmlControls; 13
14
namespace _1aspxUploads 15
{ 16
//51aspx.com多文件上传示例 17
public partial class _Default : System.Web.UI.Page 18
{ 19
static public ArrayList hif = new ArrayList(); // 保存文件列表 20
public int filesUploaded = 0; // 上传文件的数量 21
22
protected void Page_Load(object sender, EventArgs e) 23
{ 24
25
} 26
/// <summary> 27
/// 将要上传的文件添加到listbox中 28
/// </summary> 29
/// <param name="sender"></param> 30
/// <param name="e"></param> 31
32
protected void AddFile_Click(object sender, EventArgs e) 33
{ 34
if (Page.IsPostBack == true) 35
{ 36
hif.Add(FindFile); 37
FileList.Items.Add(FindFile.PostedFile.FileName); 38
} 39
else 40
{ } 41
42
} 43
44
/// <summary> 45
/// 从listbox中删除指定的文件 46
/// </summary> 47
/// <param name="sender"></param> 48
/// <param name="e"></param> 49
protected void RemvFile_Click(object sender, EventArgs e) 50
{ 51
if (FileList.SelectedIndex == -1) 52
{ 53
TipInfo.Text = "错误 - 必须指定要删除的文件."; 54
return; 55
} 56
else if (FileList.Items.Count != 0) 57
{ 58
hif.RemoveAt(FileList.SelectedIndex); 59
FileList.Items.Remove(FileList.SelectedItem.Text); 60
TipInfo.Text = ""; 61
} 62
63
} 64
65
/// <summary> 66
/// 循环上传listbox中的文件到指定的文件夹下 67
/// </summary> 68
/// <param name="sender"></param> 69
/// <param name="e"></param> 70
public void Upload_ServerClick(object sender, System.EventArgs e) 71
{ 72
string baseLocation = Server.MapPath("UploadFiles/"); // 上传路径 73
string status = ""; // 上传成功后显示的文件列表 74
75
if ((FileList.Items.Count == 0) && (filesUploaded == 0)) 76
{ 77
TipInfo.Text = "错误 - 必须指定要上传的文件."; 78
return; 79
} 80
else 81
{ 82
foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif) 83
{ 84
try 85
{ 86
string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName); 87
HIF.PostedFile.SaveAs(baseLocation + fn); 88
filesUploaded++; 89
status += fn + "<br>"; 90
} 91
catch (Exception err) 92
{ 93
TipInfo.Text = "上传错误 " + baseLocation 94
+ "<br>" + err.ToString(); 95
} 96
} 97
98
if (filesUploaded == hif.Count) 99
{ 100
TipInfo.Text = "共上传了 " + filesUploaded + " 个文件。 <br>" + status; 101
} 102
hif.Clear(); 103
FileList.Items.Clear(); 104
} 105
106
} 107
} 108
} 109






}
}