Image Uploading In Data base MVC

Code Of Controller :


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

        [HttpPost]
        public ActionResult imageUpload(tbl_ImageUpload imagemodel , HttpPostedFileBase file)//imagemodel is object of imageUpload Model and File is Object off HttppostFileBase File Is same name or id of button mostly importantn
        {
            TestDbEntities db = new TestDbEntities();//object of database file entity
            tbl_ImageUpload tbl = new tbl_ImageUpload();//object of table
            var allowedExtention = new[] { ".JPG",".PNG",".JPG",".PNG",".GIF",".WEBP",".TIFF",".PSD",".RAW",".BMP",".HEIF",".INDD", ".Png", ".jpg", "  jpeg" };//set the Extention
            tbl.Description = imagemodel.Description;//Other filed of table
            tbl.image = file.ToString();//Geting the full url of image
            var filename = Path.GetFileName(file.FileName);//Getting the only name of file
            var ext = Path.GetExtension(file.FileName);//getting the Extentiong of image
            if(allowedExtention.Contains(ext))//checking the Extention of file 
            {
                string name = Path.GetFileNameWithoutExtension(filename);//getting the name of image without extention
                string myfile = name + "_" + tbl.imageid + ext;//Join File name with the Image id 
                var path = Path.Combine(Server.MapPath("~/img"), myfile);//set the path of image saving loacation 
                tbl.image = path;//set path and insert in database
                tbl.Description = imagemodel.Description;//Other filed of table
                db.tbl_ImageUpload.Add(tbl);//add image path and other fileds in database
                db.SaveChanges();//save Database changes
                file.SaveAs(path);//file saveing on path
                ModelState.Clear();
            }
            else
            {
                ViewBag.message = "Pleace select only image file";
            }

            return View();
        }

Model Code :


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

namespace ImageUpload.Models
{
    public class Myimagemodel
    {
        public int imageid { get; set; }
        public string Description { get; set; }
        public string image { get; set; }
    }
}


csHTML  :

@model ImageUpload.Models.Myimagemodel

@{
    ViewBag.Title = "imageUpload";
}
@ViewBag.message
@ViewBag.msg
<h2>imageUpload</h2>


@using (Html.BeginForm("imageUpload", "Home", FormMethod.Post, new{ enctype = "multipart/form-data"}))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Myimagemodel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @*@Html.EditorFor(model => model.image, new { htmlAttributes = new { @class = "form-control" } })*@
                <input type="file" id="file" name="file" class="btn btn-success" />
                @Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" id="imgfile" name="imgfile" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

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