Easy And Best Way to upload image in DataBase --MVC

Controller Code:

   [HttpGet]

        public ActionResult CreateAccount()
        {
            return View();
        }

        [Route("CreateNewAccount")]
        [HttpPost]

        public ActionResult CreateAccount(MyLogin log , HttpPostedFileBase file)
        {

            FormDbEntities db = new FormDbEntities();
            var userdata = db.tbl_Login.Where(x => x.Userid == log.Userid).Count();//cheking for not use same user name
            if(userdata>0)
            {
                ViewData["usererro"] = "This User name Alredy used";
            }
            else
            {

                
                var Gmaildata = db.tbl_Login.Where(x => x.Gmail == log.Gmail).Count();
                if(Gmaildata>0)
                {
                    ViewData["Gmailerror"] = "This Gmail alredy used";
                }
                else
                {
                    tbl_Login tb = new tbl_Login();
                    if(file !=null && file.ContentLength>0)
                    {
                        var allowedExtention = new[] { ".JPG", ".PNG", ".JPG", ".PNG", ".GIF", ".WEBP", ".TIFF", ".PSD", ".RAW", ".BMP", ".HEIF", ".INDD", ".Png", ".jpg", "  jpeg" };//set the Extention
                        var ext = Path.GetExtension(file.FileName);//getting the Extentiong of image
                        if(allowedExtention.Contains(ext))
                        {
                            string filename = file.FileName;
                            string filepath = string.Format("/Content/Registerd_img/{0}", filename);//database to save path
                            string pathTosave = string.Format(@"{0}", Server.MapPath("~/Content/Registerd_img/"));
                            pathTosave = pathTosave + @"\" + filename;
                            file.SaveAs(pathTosave);
                            // tb.ImagePath = filename;



                            tb.FirstName = log.FirstName;
                            tb.Userid = log.Userid;
                            tb.password = log.password;
                            tb.Copassword = log.Copassword;
                            tb.Contactno = log.Contactno;
                            tb.Gmail = log.Gmail;
                            tb.ImagePath = filename.ToString();
                            db.tbl_Login.Add(tb);
                            int a = db.SaveChanges();
                            //int a=db.SaveChanges();
                            if (a == 1)
                            {
                                Session["data"] = "Welcome in new account" + "  " + tb.FirstName.ToString();
                                return RedirectToAction("Login");

                            }
                            else
                            {
                                ViewBag.alert = "<script>alert('Account Creation Fail')</script>";
                                ViewData["Greteuser"] = "Welcome in new account" + tb.FirstName.ToString();
                            }
                            //ViewData["Errornotimage"] = "Image uploaded";
                        }
                        else
                        {
                            ViewData["Errornotimage"] = "Pleace select image only Image File";
                        }

                       
                    }
                    else
                    {
                        ViewData["Errornotimage"] = "Pleace select image";
                    }
                    //FormDbEntities db = new FormDbEntities();
                    //db.tbl_Login.Add(tb);
                    //int a = db.SaveChanges();
                    
                }
            }
         
            return View();
        }

model code :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Test.Models
{
    public class MyLogin
    {
        [Required(ErrorMessage ="Enter First Name")]
        public string FirstName { get; set; }
        [Required(ErrorMessage ="Enter User Name")]
        [Display(Name ="User Name")]
        public string Userid { get; set; }
        [Required(ErrorMessage ="Enter password")]
        [DataType(DataType.Password)]
        public string password { get; set; }
        [Required(ErrorMessage =" Enter Conform password")]
        [DataType(DataType.Password)]
        public string Copassword { get; set; }
        [Required(ErrorMessage ="enter Countact No")]
        [Range(8888888888,9999999999)]
        public string Contactno { get; set; }
        [EmailAddress]
        [Required(ErrorMessage ="Enter Your Gmail")]
        public string Gmail { get; set; }
        [Required(ErrorMessage ="pleace Upload Your image ")]
        public string ImagePath { get; set; }
    }
}


csHTML  :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Test.Models
{
    public class MyLogin
    {
        [Required(ErrorMessage ="Enter First Name")]
        public string FirstName { get; set; }
        [Required(ErrorMessage ="Enter User Name")]
        [Display(Name ="User Name")]
        public string Userid { get; set; }
        [Required(ErrorMessage ="Enter password")]
        [DataType(DataType.Password)]
        public string password { get; set; }
        [Required(ErrorMessage =" Enter Conform password")]
        [DataType(DataType.Password)]
        public string Copassword { get; set; }
        [Required(ErrorMessage ="enter Countact No")]
        [Range(8888888888,9999999999)]
        public string Contactno { get; set; }
        [EmailAddress]
        [Required(ErrorMessage ="Enter Your Gmail")]
        public string Gmail { get; set; }
        [Required(ErrorMessage ="pleace Upload Your image ")]
        public string ImagePath { get; set; }
    }
}

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

How To insert Radio Button Data in DataBase -- MVC

Toolbox Webparts Controls