jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

首先在页面加一个上传文件控件,一个“attach"按钮,一个listbox,用来存放等待上传的文件名,
一个"UPLOAD"按钮,一个”删除按钮
 <form id="Form1" method="post" runat="server">
   <INPUT id="FileUpload" style="Z-INDEX: 101; LEFT: 83px; WIDTH: 489px; POSITION: absolute; TOP: 67px; HEIGHT: 22px"
  type="file" size="62" runat="server">
   <asp:button id="btnAttach" style="Z-INDEX: 102; LEFT: 591px; POSITION: absolute; TOP: 66px"
    runat="server" Text="Attach"></asp:button><asp:listbox id="ListBox1" style="Z-INDEX: 103; LEFT: 84px; POSITION: absolute; TOP: 104px" runat="server"
    Width="565px" Height="93px"></asp:listbox><asp:button id="btnUpload" style="Z-INDEX: 104; LEFT: 91px; POSITION: absolute; TOP: 198px"
    runat="server" Text="Upload"></asp:button><asp:button id="btnDelete" style="Z-INDEX: 105; LEFT: 684px; POSITION: absolute; TOP: 131px"
    runat="server" Text="Delete" Width="58px"></asp:button>
   <asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 166px; POSITION: absolute; TOP: 199px" runat="server"
    Width="476px" ForeColor="Red"></asp:Label></form>

之后,在"attach"按钮中代码如下:
private void btnAttach_Click(object sender, System.EventArgs e)
  {
   // Save the attached file to fileName variable
   string fileName = FileUpload.PostedFile.FileName;

   // If the counter is null then create one with default value equal to 0
   if(ViewState["i"] == null)
   {
    ViewState["i"]= 0;
   }
   // Check if a file is selected
   if(fileName != null || fileName != string.Empty)
   {
    // Add  it to the collection
    ListBox1.Items.Add(FileUpload.PostedFile.FileName);

    // Save an index for each selected file
    int i = Convert.ToInt32(ViewState["i"]);

    // Save the fileupload control into a different session
    Session["myupload" + i] = FileUpload;

    // Increment the counter
    i++;

    // Set the ViewSate to the latest counter value.
    ViewState["i"] = i;
    
   }
  }

很明显,其实是用viewstate来存放用户上传的实际文件名,这需要用户在选择文件后用
"attach"按钮来将文件添加到那个listbox框中去
之后是“upload"的代码,
 private void btnUpload_Click(object sender, System.EventArgs e)
  {
   int sessionCount = Session.Count;
   
   
   ///int sessionCount = Convert.ToInt32(ViewState["i"]);
   for( int i =sessionCount-1;i>=0;i--)
   {
    if(sessionCount <= 3)
    {
     HtmlInputFile hif = (HtmlInputFile)Session["myupload" + i];
     if(hif.PostedFile.ContentLength <= 500000)
     {
      string storePath = Server.MapPath("~") + "/MultipleUpload";
      if(!Directory.Exists(storePath))
       Directory.CreateDirectory(storePath);
      hif.PostedFile.SaveAs(storePath  + "/" +  Path.GetFileName(hif.PostedFile.FileName));
      Label1.Text = "Your Files are uploaded successfully";
      ListBox1.Items.Clear();
     }
     else
     Label1.Text = "An error occured";
    }
    else
     Label1.Text = "You have exceeded the maximum number of files to be uploaded (3)";
     
   }
   Session.RemoveAll();
  }

   实际上是将所有的要上传的文件从session里取回来,然后每一个逐一上传
最后是把要上传的文件从listbox里删除的代码,在session里remove掉
private void btnDelete_Click(object sender, System.EventArgs e)
  {
   if(ListBox1.SelectedIndex > -1)
   {
    int uploadedFileIndex = ListBox1.SelectedIndex;
    Session.Remove("myupload" + uploadedFileIndex);
    ListBox1.Items.Remove(ListBox1.SelectedValue);
   }
  }

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




posted on 2007-04-12 16:09  jackyrong的世界  阅读(2898)  评论(2编辑  收藏  举报