[Solved] Uploading Multiple Files using ASP.Net FileUpload Control saves one File in Folder (Directory) in C#

Last Reply one month ago By dharmendr

Posted one month ago

i am uploading 2 files say a.pdf and b.pdf and i am clicking on update button.I can see only a.pdf into agreement folder.It does not upload b.pdf.My VS version is below 4.0,so i used below code.

During debugging.i found that it is going through the loop but both times it takes a.pdf and skipping b.pdf as i selected both.

Source code:

<tr>
    <td class="style11">
        Reply recieved date<br />
        <asp:TextBox ID="TextBox1" Width="207" class="datepicker" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="valBtoA"
            ControlToValidate="txtrecdate" runat="server" ForeColor="red" ErrorMessage="reply date!!"></asp:RequiredFieldValidator>
    </td>
    <td class="style3">
        Agreement
        <asp:RadioButton ID="RadioButton1" GroupName="agree" AutoPostBack="true" OnCheckedChanged="enablefileuploader"
            runat="server" />
        Yes
        <asp:RadioButton ID="RadioButton2" GroupName="agree" AutoPostBack="true" OnCheckedChanged="disablefileuploader"
            runat="server" />
        No&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a id="a1" target="_blank" runat="server">
            <asp:Label ID="Label1" ForeColor="blue" runat="server"></asp:Label>
        </a>
        <br />
    </td>
</tr>
<tr>
    <asp:FileUpload ID="FileUpload2" runat="server" accept=".pdf,.PDF" AllowMultiple="true" />
    &nbsp;&nbsp;
    <asp:Label ID="Label2" runat="server" ForeColor="red"></asp:Label>
    </td>
    <td class="style12">
        Comment2<br />
        <asp:TextBox ID="TextBox2" TextMode="MultiLine" Width="207" runat="server"></asp:TextBox>
    </td>
</tr>

 Code behind: 

protected void btnupdate_Click(object sender, EventArgs e)//work
{
    string x = Request.QueryString["pid"];
    string queryStr = "", rrdate = "", compltdate = "", filename = "", agreement = "", lspdate = "", email2 = "", email3 = "", reason = "";
    MySqlConnection connect = null;
    MySqlTransaction transaction;
    string connStr = ConfigurationManager.ConnectionStrings["BarterConnectionString"].ToString();
    connect = new MySqlConnection(connStr);
    connect.Open();
    if (rbyes.Checked)
    {
        if (FileUpload1.HasFile)
        {
            string FileName = System.IO.Path.GetFileName(FileUpload1.FileName);
            HttpFileCollection uploadedFiles = Request.Files;
            lblfileupmsg.Text = string.Empty;

            for (int i = 0; i < uploadedFiles.Count; i++)
            {
                HttpPostedFile userPostedFile = uploadedFiles[i];

                try
                {
                    if (userPostedFile.ContentLength > 0)
                    {
                        lblfileupmsg.Text += "<u>File #" + (i + 1) + "</u><br>";
                        lblfileupmsg.Text += "File Content Type: " + userPostedFile.ContentType + "<br>";
                        lblfileupmsg.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>";
                        lblfileupmsg.Text += "File Name: " + userPostedFile.FileName + "<br>";

                        userPostedFile.SaveAs(Server.MapPath("~/Agreements/" + FileName));
                        lblfileupmsg.Text += "Location where saved: " + FileName + "~/Agreements/" + Path.GetFileName(userPostedFile.FileName) + "<p>";
                    }
                }
                catch (Exception Ex)
                {
                    lblfileupmsg.Text += "Error: <br>" + Ex.Message;
                }
            }
        }
        else
        {
            filename = ViewState["filename"].ToString();
            if (filename == "")
            {
                lblfileupmsg.Text = "Please Upload Agreement file!!!";
                return;
            }
            agreement = "1";
        }

    }
}

 

Posted one month ago
chetan says:
if (rbyes.Checked)
    {
        if (FileUpload1.HasFile)
        {
            string FileName = System.IO.Path.GetFileName(FileUpload1.FileName);
            HttpFileCollection uploadedFiles = Request.Files;
            lblfileupmsg.Text = string.Empty;
 
            for (int i = 0; i < uploadedFiles.Count; i++)
            {
                HttpPostedFile userPostedFile = uploadedFiles[i];
 
                try
                {
                    if (userPostedFile.ContentLength > 0)
                    {
                        lblfileupmsg.Text += "<u>File #" + (i + 1) + "</u><br>";
                        lblfileupmsg.Text += "File Content Type: " + userPostedFile.ContentType + "<br>";
                        lblfileupmsg.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>";
                        lblfileupmsg.Text += "File Name: " + userPostedFile.FileName + "<br>";
 
                        userPostedFile.SaveAs(Server.MapPath("~/Agreements/" + FileName));
                        lblfileupmsg.Text += "Location where saved: " + FileName + "~/Agreements/" + Path.GetFileName(userPostedFile.FileName) + "<p>";
                    }
                }
                catch (Exception Ex)
                {
                    lblfileupmsg.Text += "Error: <br>" + Ex.Message;
                }
            }
        }

There is no problem with your code. You just need to declare the FileName inside the loop and get the file name from the HttpPostedFile.

Refer the below modified code.

if (rbyes.Checked)
{
    if (FileUpload1.HasFile)
    {
        HttpFileCollection uploadedFiles = Request.Files;
        lblfileupmsg.Text = string.Empty;

        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[i];
            string FileName = System.IO.Path.GetFileName(userPostedFile.FileName);
            try
            {
                if (userPostedFile.ContentLength > 0)
                {
                    lblfileupmsg.Text += "<u>File #" + (i + 1) + "</u><br>";
                    lblfileupmsg.Text += "File Content Type: " + userPostedFile.ContentType + "<br>";
                    lblfileupmsg.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>";
                    lblfileupmsg.Text += "File Name: " + FileName + "<br>";

                    userPostedFile.SaveAs(Server.MapPath("~/Agreements/" + FileName));
                    lblfileupmsg.Text += "Location where saved: " + Server.MapPath("~/Agreements/" + FileName) + "<p>";
                }
            }
            catch (Exception Ex)
            {
                lblfileupmsg.Text += "Error: <br>" + Ex.Message;
            }
        }
    }