How to upload multiple photos using fileupload control in ASP.Net

Last Reply 4 months ago By cywa

Posted 5 months ago

Hi,

I want to upload multiple photos with mulitiple fileupload control in sqlserver database but the end on one upload button click.
please help me to find this.

Thanks and regards,

cywa

Posted 5 months ago Modified on 5 months ago

Hi cywa,

I have created a sample which full fill your requirement

By taking reference of below article

Save and Retrieve images from database in ASP.Net

You need to modify the code according to your requirement

HTML

<div>
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:FileUpload ID="FileUpload2" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:FileUpload ID="FileUpload3" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnUpload" runat="server" Text="upload" Style="width: 30%;" OnClick="btnUpload_Click" />
            </td>
        </tr>
    </table>
    <asp:Label ID="lblMessage" runat="server" />
</div>

C#

protected void btnUpload_Click(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/Images/");
    if (FileUpload1.HasFile && FileUpload2.HasFile && FileUpload3.HasFile)
    {
        string fileUpload1 = Path.GetFileName(FileUpload1.FileName);
        FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
        InsertImages(fileUpload1, FileUpload1.PostedFile.ContentType, folderPath + fileUpload1);

        string fileUpload2 = Path.GetFileName(FileUpload2.FileName);
        FileUpload2.SaveAs(folderPath + Path.GetFileName(FileUpload2.FileName));
        InsertImages(fileUpload2, FileUpload2.PostedFile.ContentType, folderPath + fileUpload2);

        string fileUpload3 = Path.GetFileName(FileUpload3.FileName);
        FileUpload3.SaveAs(folderPath + Path.GetFileName(FileUpload3.FileName));
        InsertImages(fileUpload3, FileUpload3.PostedFile.ContentType, folderPath + fileUpload3);

        lblMessage.ForeColor = Color.Green;
        lblMessage.Text = "All Files Uploaded and Saved Successfully in DataBase.";
    }
    else
    {
        lblMessage.ForeColor = Color.Red;
        lblMessage.Text = "Please Upload All Files";
    }
}

public void InsertImages(string fileName, string contentType, string filePath)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        string query = "INSERT INTO tblFiles VALUES(@Name,@ContentType,@Data,@FilePath)";
        using (SqlCommand cmd = new SqlCommand(query))
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            br.Close();
            fs.Close();
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Name", fileName);
            cmd.Parameters.AddWithValue("@FilePath", filePath);
            cmd.Parameters.AddWithValue("@Data", bytes);
            cmd.Parameters.AddWithValue("@ContentType", contentType);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

ScreenShot

 

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108