locked
how to upload multiple files at a time? RRS feed

  • Question

  • User1288863645 posted

    how can we upload multiple files at a time...

    my requirement is, when i click on browse buttton, i shud be able to select multiple images at a time, not clicking browse button every time for every image nd uploading all at once

    then i shud be able to use upload button, i want event handler in upload button

    lot of application in net are using flash buttons for uploading button, or clicking on browse button every time for selecting every image, or when we select multiple images, directly they are getting uploaded with the use of upload button...

    thanks in advance

    Wednesday, September 21, 2011 12:42 AM

Answers

  • User3866881 posted

    Hello alwaysonline:)
    In fact I think you can try this:

    1) Drag and drop a GridView and set two fields: One is checkboxfield, while the other is a common BoundField.

    <asp:GridView ……>

       <Columns>

             <asp:Template HeaderText="Choose Files">

                  <ItemTemplate>

                       <asp:CheckBox Id="chkBox" runat="server"/>

                  </ItemTemplate>

             </asp:Template>

               <asp:Template HeaderText="File Path">

                  <ItemTemplate>

                       <asp:Label Id="lbFilePath" runat="server" Text='<%#Eval("FilePath")%>'/>

                  </ItemTemplate>

             </asp:Template>

       </Columns>

    </asp:GridView>

    <asp:Button Id="btnSubmit" runat="server" Text="Upload All" />

    Your codes behind:

    var result = from path in Directory.GetFiles("Your File Path From your PC","*.*",SearchOption.AllDirectories);
                      select new
                      {
                            FilePath = path
                      };

    GridView.DataSource = result;

    GridView.DataBind();

    And in the end, double click the button and say——

    FileUpLoad fp = new FileUpLoad();
    foreach(GridViewRow row in GridView.Rows)
    {
          CheckBox chkbox = (CheckBox)row.FindControl("chkBox");
           if(chkbox.Checked)
           {
               Label lb = (Label)row.FindControl("lbFilePath");
               fp.SaveAs(Server.MapPath("~/Files/")+new FileInfo(lb.Text).FileName;
           }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2011 2:51 AM

All replies

  • User-1942430961 posted

    Hi ,

    USE Yahoo Yui Toolkit (multiple file uploader)

    http://developer.yahoo.com/yui/examples/uploader/uploader-advanced-queue.html

    or use Silverlight multiple file uploader.

    Wednesday, September 21, 2011 12:57 AM
  • User3866881 posted

    Hello alwaysoline,

    protected void btnUpload_Click(object sender, EventArgse)

        {
            try
            {
                // Get the HttpFileCollection
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("MyFiles") + "\\" +
                          System.IO.Path.GetFileName(hpf.FileName));
                        Response.Write("<b>File: </b>" + hpf.FileName + " <b>Size:</b> " +
                            hpf.ContentLength + " <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>");
                    }
                }
            }
            catch (Exception ex)
            {
               
            }
        }
    Thursday, September 22, 2011 9:31 PM
  • User1288863645 posted

    not the above one, we shud be able to select multiple images in a single browse click nd then upload

    Friday, September 23, 2011 2:25 AM
  • User3866881 posted

    Hello alwaysonline:)
    In fact I think you can try this:

    1) Drag and drop a GridView and set two fields: One is checkboxfield, while the other is a common BoundField.

    <asp:GridView ……>

       <Columns>

             <asp:Template HeaderText="Choose Files">

                  <ItemTemplate>

                       <asp:CheckBox Id="chkBox" runat="server"/>

                  </ItemTemplate>

             </asp:Template>

               <asp:Template HeaderText="File Path">

                  <ItemTemplate>

                       <asp:Label Id="lbFilePath" runat="server" Text='<%#Eval("FilePath")%>'/>

                  </ItemTemplate>

             </asp:Template>

       </Columns>

    </asp:GridView>

    <asp:Button Id="btnSubmit" runat="server" Text="Upload All" />

    Your codes behind:

    var result = from path in Directory.GetFiles("Your File Path From your PC","*.*",SearchOption.AllDirectories);
                      select new
                      {
                            FilePath = path
                      };

    GridView.DataSource = result;

    GridView.DataBind();

    And in the end, double click the button and say——

    FileUpLoad fp = new FileUpLoad();
    foreach(GridViewRow row in GridView.Rows)
    {
          CheckBox chkbox = (CheckBox)row.FindControl("chkBox");
           if(chkbox.Checked)
           {
               Label lb = (Label)row.FindControl("lbFilePath");
               fp.SaveAs(Server.MapPath("~/Files/")+new FileInfo(lb.Text).FileName;
           }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2011 2:51 AM
  • User-1002157272 posted

    Nice! Awesome solution! I hate when people always just simply suggest some third-party control when you really want a solution you can develop yourself. Great suggestion and I think  may check this out too.

    Another solution I was thinking of trying was the following:

    adding a set number of fileupload controls, lets say 5 for an example.

    then on click of the upload button, get the 5 controls from the control container and insert them into a List<FileUpload> and create a method that accepts this list as a parameter. In the method do a foreach loop that performs all the validation on each control's filestream and, if valid, performs the upload. Could even get fancy if you wanted and run each upload on it's own background thread asynchronously.

    I like the method you described though as it allows you to bind the gridview to the files on the pc. One question though, to allow the gridview to be bound to the filepath from the client machine, does it require any permissions? I mean will it ask the user to allow the page to access local files or will it just do it automatically? I never really tried it before

    Thursday, November 10, 2011 11:09 PM
  • User-896545167 posted

    Hello,

    When I try your solution in IE9, on either single or multiple file, IE9 just replaces the contents with the first file being dropped, and all else vanishes. I must be doing some minor error. Besides, your code does not work straight off the box. I had to resort to some modifications, but I do not think they are to blame, since they come after the files have been dropped.

    There must be something trivial I am doing wrong. I even tried it in Firefox, with the same result, i.e. the first file dropped fills the browser screen. What are the others doing that I am not?

    I can post my code, but await the 2 second answer first :-)

    Saturday, April 13, 2013 2:51 AM