Thursday 13 January 2022

Multiple file upload by form post ASP.NET C# MVC


Multiple file upload by form post ASP.NET C# MVC


This post will let you upload multiple image files through a single form post using the C# ASP.Net MVC platform.

Key Point

  • Upload multiple image files with a new name
  • Save a associate record and file name will be newly created record identity field value
  • No need to save the image name in the database because the file name will start with a record ID and end with a text. 
  • Best for putting student, employee documents without any hassle. 

HTML Code : 

<form method="post" enctype="multipart/form-data">

     @Html.AntiForgeryToken()

    <p class="m-0">

        Photo

        <input type="file" name="Photo" id="Photo" accept="image/x-png,image/jpeg" />

        <br />

        Adhaar

        <input type="file" name="Adhaar" id="Adhaar" accept="image/x-png,image/jpeg" />

        <br />

        Pan

        <input type="file" name="Pan" id="Pan" accept="image/x-png,image/jpeg" />

        <br />

        <input type="checkbox" id="chkconfirm" /> I provide my consent. I will follow the Tangentweb term condition and policy.

        <br /> <br />

        <input type="submit" value="Save" class="button button-primary button-wide-mobile" onclick="return filecheck()" />

    </p> 

</form>

Java Script

function filecheck()
{

        Flag = true;
    if (checkfile('Photo') == false)
      {
        alert('Maximum 3MB file size is for Photo');
        Flag = false;
       }

    if (checkfile('Adhaar') == false)
    {
        alert('Maximum 3MB file size is for Aadhar');
        Flag = false;
    }

    if (checkfile('Pan') == false)
    {
        alert('Maximum 3MB file size is for Pan');
        Flag = false; 

    }  

    return Flag;

}


Server side Code

[HttpPost]
[ValidateAntiForgeryToken]

        public ActionResult Index(ContactModel data)
        {

            if (ModelState.IsValid)
            {

                string ErrMsg = "", sql = "",recid="";              

        sql = "insert into stu (Name, Phone ) values('" + data.Name + "','" + data.Phone + "')";

                InsertRepository ob = new InsertRepository();
                if (ob.SaveRegistration(sql,ref recid, ref ErrMsg))
                {

                    HttpFileCollectionBase files = Request.Files;

                    HttpPostedFileBase file = files["Photo"];

                    if(file.FileName!="")                        file.SaveAs(Server.MapPath("~/AppImage/document/" + recid + "_photo.jpg"));

                    file = files["Adhaar"];

                    if (file.FileName != "")

                        file.SaveAs(Server.MapPath("~/AppImage/document/" + recid + "_aadhar.jpg"));

                     file = files["Pan"];

                    if (file.FileName != "")

                        file.SaveAs(Server.MapPath("~/AppImage/document/" + recid + "_pan.jpg"));

 

                    return RedirectToAction("thanks", "home", new { msg = "Registration record saved successfully . We will contact you shortly",amt=data.PaymentAmount });

                }                   

                else

                    return RedirectToAction("err", "home", new { msg = ErrMsg });

            }

            else

                return RedirectToAction("err", "common", new { msg = "Bots" });
        }

Repository

public bool SaveRegistration(string sql,ref string recid, ref string ErrMsg)
{
            String result = "";
            try
            {
                using (SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, dbcon))
                    {
                        if (dbcon.State == ConnectionState.Closed)
                            dbcon.Open();

                        cmd.ExecuteNonQuery();
                        cmd.CommandText = "select IDENT_CURRENT('[dbo].[talenthunt]')";
                        recid = cmd.ExecuteScalar().ToString();
                        result = "1";

                    }

                } 

            }
            catch (Exception ex)
            {

                ErrMsg = ex.Message;

            }

            if (result == "1")
                return true;
            else
                return false;
}

 Next Post

No comments:

Post a Comment