locked
how to upload multiple files in fileupload control c# .net RRS feed

  • Question

  • User1046245955 posted

    how to upload a multiple files in asp.fileupload control

    here's my code:

    <asp:FileUpload ID="FileUploadControl" runat="server" CssClass="form-control" Width="100%" />

    code behind:

    private void UploadImageFile()
    {

    string uploadFolder = @"//server/foldername/" + "ArchivingFiles//";

    if (FileUploadControl.HasFile)
    {
    string extension = Path.GetExtension(FileUploadControl.PostedFile.FileName);

    if (extension != ".pdf")
    {
    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Attached a PDF File only.');", true);
    return;
    }

    FileUploadControl.SaveAs(uploadFolder + DateTime.Now.ToString("ddMMhhmm") + sqno + extension);

    filename = DateTime.Now.ToString("ddMMhhmm") + sqno + extension;

    imLocation = uploadFolder + filename;

    FileUploadControl.SaveAs(imLocation);
    this.AddtoAttachmentsList(filename);

    }
    else
    {
    }

    }

    private void AddtoAttachmentsList(string fn)
    {

    insertdb query.....

    }

    Wednesday, August 21, 2019 7:53 AM

Answers

  • User288213138 posted

    Hi paminchever,

    According to your description, I made a demo for your reference.

    If you want to upload multiple files in fileupload control ,you can set the AllowMultiple=”true”.

    The code:

    <div>
                <asp:FileUpload ID="FileUploadControl" runat="server" AllowMultiple="true" />
                <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="UploadMultipleFiles"/>
                <hr />
                <asp:Label ID="lblSuccess" runat="server" ForeColor="Green" />
            </div>
    protected void UploadMultipleFiles(object sender, EventArgs e)
            {
                foreach (HttpPostedFile postedFile in FileUploadControl.PostedFiles)
                {
                    string extension = Path.GetExtension(FileUploadControl.PostedFile.FileName);
                    if (extension != ".PDF")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Attached a PDF File only.');", true);
                        return;
                    }
                    string fileName = Path.GetFileName(postedFile.FileName);
                    postedFile.SaveAs(Server.MapPath("~/Doc/") + DateTime.Now.ToString("ddMMhhmm") + fileName + extension);
                }
                lblSuccess.Text = string.Format("{0} files have been uploaded successfully.", FileUploadControl.PostedFiles.Count);
            }
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2019 8:47 AM