Zrlhappy的博客
                               从今天起, 做一个幸福的人
                                    喂马, 劈柴, 周游世界
                                    从今天起, 关心粮食和蔬菜
                                    我有一所房子, 面朝大海, 春暖花开
                                    给每一条河每一座山取一个温暖的名字
                                    我也愿面朝大海, 春暖花开……

在asp.net 中,如何先让用户把要上传的文件都选好了,然后一次上传,今小结如下

首先在页面加一个上传文件控件,一个“attach"按钮,一个listbox,用来存放等待上传的文件名,
一个"UPLOAD"按钮,一个”删除按钮

 1<form id="Form1" method="post" runat="server">
 2   <INPUT id="FileUpload" style="Z-INDEX: 101; LEFT: 83px; WIDTH: 489px; POSITION: absolute; TOP: 67px; HEIGHT: 22px"
 3  type="file" size="62" runat="server">
 4   <asp:button id="btnAttach" style="Z-INDEX: 102; LEFT: 591px; POSITION: absolute; TOP: 66px"
 5    runat="server" Text="Attach"></asp:button><asp:listbox id="ListBox1" style="Z-INDEX: 103; LEFT: 84px; POSITION: absolute; TOP: 104px" runat="server"
 6    Width="565px" Height="93px"></asp:listbox><asp:button id="btnUpload" style="Z-INDEX: 104; LEFT: 91px; POSITION: absolute; TOP: 198px"
 7    runat="server" Text="Upload"></asp:button><asp:button id="btnDelete" style="Z-INDEX: 105; LEFT: 684px; POSITION: absolute; TOP: 131px"
 8    runat="server" Text="Delete" Width="58px"></asp:button>
 9   <asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 166px; POSITION: absolute; TOP: 199px" runat="server"
10    Width="476px" ForeColor="Red"></asp:Label></form>
11


之后,在"attach"按钮中代码如下:

 1private void btnAttach_Click(object sender, System.EventArgs e)
 2  {
 3   // Save the attached file to fileName variable
 4   string fileName = FileUpload.PostedFile.FileName;
 5
 6   // If the counter is null then create one with default value equal to 0
 7   if(ViewState["i"== null)
 8   {
 9    ViewState["i"]= 0;
10   }

11   // Check if a file is selected 
12   if(fileName != null || fileName != string.Empty)
13   {
14    // Add  it to the collection
15    ListBox1.Items.Add(FileUpload.PostedFile.FileName);
16
17    // Save an index for each selected file
18    int i = Convert.ToInt32(ViewState["i"]);
19
20    // Save the fileupload control into a different session
21    Session["myupload" + i] = FileUpload;
22
23    // Increment the counter
24    i++;
25
26    // Set the ViewSate to the latest counter value.
27    ViewState["i"= i;
28    
29   }

30  }

很明显,其实是用viewstate来存放用户上传的实际文件名,这需要用户在选择文件后用
"attach"按钮来将文件添加到那个listbox框中去
之后是“upload"的代码,

 1private void btnUpload_Click(object sender, System.EventArgs e)
 2  {
 3   int sessionCount = Session.Count;
 4   
 5   
 6   ///int sessionCount = Convert.ToInt32(ViewState["i"]);
 7   forint i =sessionCount-1;i>=0;i--)
 8   {
 9    if(sessionCount <= 3)
10    {
11     HtmlInputFile hif = (HtmlInputFile)Session["myupload" + i];
12     if(hif.PostedFile.ContentLength <= 500000)
13     {
14      string storePath = Server.MapPath("~"+ "/MultipleUpload";
15      if(!Directory.Exists(storePath))
16       Directory.CreateDirectory(storePath);
17      hif.PostedFile.SaveAs(storePath  + "/" +  Path.GetFileName(hif.PostedFile.FileName));
18      Label1.Text = "Your Files are uploaded successfully";
19      ListBox1.Items.Clear();
20     }

21     else
22     Label1.Text = "An error occured";
23    }

24    else
25     Label1.Text = "You have exceeded the maximum number of files to be uploaded (3)";
26     
27   }

28   Session.RemoveAll();
29  }

30

        实际上是将所有的要上传的文件从session里取回来,然后每一个逐一上传
最后是把要上传的文件从listbox里删除的代码,在session里remove掉

 1private void btnDelete_Click(object sender, System.EventArgs e)
 2  {
 3   if(ListBox1.SelectedIndex > -1)
 4   {
 5    int uploadedFileIndex = ListBox1.SelectedIndex;
 6    Session.Remove("myupload" + uploadedFileIndex);
 7    ListBox1.Items.Remove(ListBox1.SelectedValue);
 8   }

 9  }

10

  总结一下,用在上传文件不多的情况下比较好,因为要用session

posted on 2007-04-12 17:35  zrlhappy  阅读(256)  评论(0编辑  收藏  举报