利用ASP.NET的Listbox將要upload的檔案列出來,並且一次上傳多個檔案
最近看到一個不錯的範例...可以解決多個檔案上傳的問題...
在CodeProject看到的...還不錯..分享給大家呀....
參考來源:Upload multiple files Hotmail style
下面是小弟參考它的程式碼再做一次的範例(c#)
MultiFileUpload.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileUpload.aspx.cs"
02 Inherits="MultiFileUpload" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>MultiFileUpload</title>
08 </head>
09 <body>
10 <form id="form1" runat="server" enctype="multipart/form-data">
11 <div>
12 <input id="FindFile" type="file" runat="server" /><br />
13 <asp:ListBox ID="ListBox1" runat="server" Height="100px" Width="250px"></asp:ListBox><br />
14 <asp:Button ID="AddFile" runat="server" Text="Add" OnClick="AddFile_Click"></asp:Button>
15 <asp:Button ID="RemvFile" runat="server" Text="Remove" OnClick="RemvFile_Click"></asp:Button>
16 <input id="Upload" type="submit" value="Upload" runat="server" onserverclick="Upload_ServerClick" />
17 </div>
18 </form>
19 </body>
20 </html>
21
02 Inherits="MultiFileUpload" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>MultiFileUpload</title>
08 </head>
09 <body>
10 <form id="form1" runat="server" enctype="multipart/form-data">
11 <div>
12 <input id="FindFile" type="file" runat="server" /><br />
13 <asp:ListBox ID="ListBox1" runat="server" Height="100px" Width="250px"></asp:ListBox><br />
14 <asp:Button ID="AddFile" runat="server" Text="Add" OnClick="AddFile_Click"></asp:Button>
15 <asp:Button ID="RemvFile" runat="server" Text="Remove" OnClick="RemvFile_Click"></asp:Button>
16 <input id="Upload" type="submit" value="Upload" runat="server" onserverclick="Upload_ServerClick" />
17 </div>
18 </form>
19 </body>
20 </html>
21
MultiFileUpload.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class MultiFileUpload : System.Web.UI.Page
13 {
14 static public ArrayList Filelist = new ArrayList();
15
16 protected void Page_Load(object sender, EventArgs e)
17 {
18
19 }
20
21 //新增檔案清單至listbox
22 protected void AddFile_Click(object sender, EventArgs e)
23 {
24 if (this.FindFile.PostedFile.FileName != "")
25 {
26 this.ListBox1.Items.Add(this.FindFile.PostedFile.FileName);
27 Filelist.Add(FindFile);
28 }
29 }
30
31 //由listbox移除檔案清單
32 protected void RemvFile_Click(object sender, EventArgs e)
33 {
34 if (this.ListBox1.Items.Count != 0)
35 {
36 if (this.ListBox1.GetSelectedIndices().Length > 0)
37 {
38 Filelist.RemoveAt(this.ListBox1.SelectedIndex);
39 this.ListBox1.Items.Remove(this.ListBox1.SelectedItem.Text);
40 }
41 }
42 }
43 protected void Upload_ServerClick(object sender, EventArgs e)
44 {
45 string Location = Server.MapPath("~/upload/");
46
47 //upload
48 if (this.ListBox1.Items.Count > 0)
49 {
50 foreach (System.Web.UI.HtmlControls.HtmlInputFile file in Filelist)
51 {
52 try
53 {
54 string name = System.IO.Path.GetFileName(file.PostedFile.FileName);
55 file.PostedFile.SaveAs(Location + name);
56 }
57 catch (Exception err)
58 {
59 //這邊可加入上傳失敗訊息
60 }
61 }
62 Filelist.Clear();
63 this.ListBox1.Items.Clear();
64
65 //這邊可以再加入上傳成功檔案清單
66 }
67 }
68 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class MultiFileUpload : System.Web.UI.Page
13 {
14 static public ArrayList Filelist = new ArrayList();
15
16 protected void Page_Load(object sender, EventArgs e)
17 {
18
19 }
20
21 //新增檔案清單至listbox
22 protected void AddFile_Click(object sender, EventArgs e)
23 {
24 if (this.FindFile.PostedFile.FileName != "")
25 {
26 this.ListBox1.Items.Add(this.FindFile.PostedFile.FileName);
27 Filelist.Add(FindFile);
28 }
29 }
30
31 //由listbox移除檔案清單
32 protected void RemvFile_Click(object sender, EventArgs e)
33 {
34 if (this.ListBox1.Items.Count != 0)
35 {
36 if (this.ListBox1.GetSelectedIndices().Length > 0)
37 {
38 Filelist.RemoveAt(this.ListBox1.SelectedIndex);
39 this.ListBox1.Items.Remove(this.ListBox1.SelectedItem.Text);
40 }
41 }
42 }
43 protected void Upload_ServerClick(object sender, EventArgs e)
44 {
45 string Location = Server.MapPath("~/upload/");
46
47 //upload
48 if (this.ListBox1.Items.Count > 0)
49 {
50 foreach (System.Web.UI.HtmlControls.HtmlInputFile file in Filelist)
51 {
52 try
53 {
54 string name = System.IO.Path.GetFileName(file.PostedFile.FileName);
55 file.PostedFile.SaveAs(Location + name);
56 }
57 catch (Exception err)
58 {
59 //這邊可加入上傳失敗訊息
60 }
61 }
62 Filelist.Clear();
63 this.ListBox1.Items.Clear();
64
65 //這邊可以再加入上傳成功檔案清單
66 }
67 }
68 }
執行結果: