locked
Upload multiples file RRS feed

  • Question

  • User639567535 posted

    I am trying to upload multiple files.. if he/she wants.. when they click on upload more files then want to display another upload .. and when when click on submit then want to insert data.. insert is not problem .. problem is when they click on upload more files then how to upload multiples

    HTML

    <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
    
      <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Upload more files</asp:LinkButton>
    <br />
    
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>

    protected void Button1_Click(object sender, EventArgs e)
            {
                var message = "";
    
                var fileName = "";
                var fileSavePath = "";
                int numFiles = Request.Files.Count;
                int uploadedCount = 0;
                for (int i = 0; i < numFiles; i++)
                {
                    var uploadedFile = Request.Files[i];
                    if (uploadedFile.ContentLength > 0)
                    {
                        fileName = Path.GetFileName(uploadedFile.FileName);
                        fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" +
                          fileName);
                        uploadedFile.SaveAs(fileSavePath);
                        uploadedCount++;
                    }
                }
                message = "File upload complete. Total files uploaded: " +
                  uploadedCount.ToString();
            }
            
    
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
    
            }

    Thursday, December 22, 2016 11:52 AM

All replies

  • User753101303 posted

    Hi,

    What if you try System.IO.Path rather than just Path? (or make sure you have a using System.IO;)

    You really want the user to explictely load a single file at a time. With 4.5 you can take advantage of HTML 5 and let the user to select multiple files with a single control:

    http://www.c-sharpcorner.com/uploadfile/99bb20/upload-multiple-files-using-fileupload-control-in-asp-net-4/

    Thursday, December 22, 2016 12:11 PM
  • User639567535 posted

    Hi,

    What if you try System.IO.Path rather than just Path? (or make sure you have a using System.IO;)

    You really want the user to explictely load a single file at a time. With 4.5 you can take advantage of HTML 5 and let the user to select multiple files with a single control:

    http://www.c-sharpcorner.com/uploadfile/99bb20/upload-multiple-files-using-fileupload-control-in-asp-net-4/

    ok i try this but when i select browse and try to select multiple files then i unable to select files .. this select only single file

    protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFiles)
                {
                    foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
                    {
                        uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"),
                        uploadedFile.FileName));
                        Span1.Text += String.Format("{0}<br />", uploadedFile.FileName);
                    }
                }

    Thursday, December 22, 2016 12:19 PM
  • User639567535 posted

    Hi,

    What if you try System.IO.Path rather than just Path? (or make sure you have a using System.IO;)

    You really want the user to explictely load a single file at a time. With 4.5 you can take advantage of HTML 5 and let the user to select multiple files with a single control:

    http://www.c-sharpcorner.com/uploadfile/99bb20/upload-multiple-files-using-fileupload-control-in-asp-net-4/

    and what if file is in different locations so when how i select files from different location/ thats why i want individual upload for every file.. when user click on upload more files then want to display file upload

    Friday, December 23, 2016 6:21 AM
  • User639567535 posted

    any help please

    Saturday, December 24, 2016 8:58 PM
  • User639567535 posted

    suppose there is file upload control.. when we select file by clicking on browse and then click on upload then file name with extension i.e. abc.doc should be display ... then when we use again same file upload and click on browse and select another file then file will be display i.e. xyz.pdf.. so there is two files abc.doc and xyz.doc .. so this is want to do .. i want to upload files and display these uploaded files

    UPDATE

    for this i try this

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            fileuploadd(FileUpload1.FileName);
    
        }
    
        public void fileuploadd(string filename)
        {
            try
            {
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
    
                        SMSEntities s = new SMSEntities();
                        uploaded_file u = new uploaded_file();
                        {
                            u.fileupload = filename;
    
    
                        }
                        s.uploaded_file.Add(u);
                        s.SaveChanges();
                    }
                    //hpf.SaveAs(Server.MapPath("upload") + "\\" + 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 (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                           ve.PropertyName, ve.ErrorMessage);
                   }
                }
                throw;
            }
    
        }

    so when i select file and click on upload then file name is display .. but when i select again file and click on upload then 1st file name is disappear and 2nd file name is display where as i want both ..

    so how i display both filenames ... and when click on submit then record files should be inserted in tables individually i am done with this

    but first i want to display both filename and then these two files should be inserte in table with indivually record like this

    1  abc.docx

    2  def.docx

    Tuesday, December 27, 2016 11:30 AM
  • User-271186128 posted

    Hi Bakhtawar Ashiq,

    so how i display both filenames ... and when click on submit then record files should be inserted in tables individually i am done with this

    but first i want to display both filename and then these two files should be inserte in table with indivually record like this

    As for this issue, it seems that you have create a new thread about this issue. You could refer to my reply in that thread and use a global list to store the upload file name.

    When you select file again, you could insert the file name into the list. Then, use the String.Join method to join the file name and display them.

    Besides, you could also use ViewState or Session to store the file name.

    Best regards,
    Dillion

    Thursday, December 29, 2016 9:16 AM