Upload image usng MVC 4

How to upload Image File using MVC 4



step 1

Add New Class in  ""Product" 

 public class Products
    {
        [Key]
        [Display(Name = "ProductId")]
        public int ProductId { get; set; }

        [Display(Name = "Name")]
        public string Name { get; set; }

        [Display(Name = "Price")]
        public string Price { get; set; }

        [Display(Name = "Image")]
        public string Image { get; set; }

        public int CREATED_BY_ID { get; set; }

        public DateTime CREATED_DATE { get; set; }

        public int UPDATED_BY_ID { get; set; }

        public DateTime UPDATED_DATE { get; set; }
    }

------ View ------ Index


@model bootstrap_searching.Models.Products
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        @Html.HiddenFor(model => model.CREATED_DATE)
        @Html.HiddenFor(model => model.UPDATED_DATE)
        @Html.HiddenFor(model => model.CREATED_BY_ID)
        @Html.HiddenFor(model => model.UPDATED_BY_ID)

        <fieldset>
            <div>
                <div>
                    @Html.LabelFor(model => model.Name)
                </div>
                <div>
                    @Html.TextBoxFor(Model => Model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
            </div>

            <div>
               <div>
                    @Html.LabelFor(model => model.Price)
                </div>
                <div>
                    @Html.TextBoxFor(Model => Model.Price)
                    @Html.ValidationMessageFor(model => model.Price)
                </div>
            </div>

            <div>
                <div>
                    @Html.LabelFor(model => model.Image)
                </div>
                <div>
                   <input type="file" name="Image" accept="image/gif, image/jpg, image/jpeg, image/png" />
                    @Html.ValidationMessageFor(model => model.Image)
                </div>
            </div>
            <div>
                <div>
                   <input type="submit" name="Save" id="submitSave" />
                </div>
            </div>
        </fieldset>
    }
</body>
</html>



---------------- end

Your page looks like this




* your controller should have action method which would accept HttpPostedFileBase;


----------- Controller ------ start

MainModel objModel = new MainModel();

[HttpGet]   
public ActionResult Index()
        {
            return View();
        }

[HttpPost]
        public ActionResult Index(Products objProducts, HttpPostedFileBase Image)
        {
            Products newProduct = new Products();

            if (Image != null)
            {
                string ext = Path.GetExtension(Image.FileName).ToLower();
                if (ext == ".gif" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp")
                {
                    string filename = "Product" + DateTime.Now.ToString().Replace(":", "").Replace("/", "").Replace(" ", "") + ext;
                    Image.SaveAs(Server.MapPath("~/UploadData/Product/") + filename);
                    objProducts.Image = "/UploadData/Product/" + filename;
                }
            }
            else
            {
                objProducts.Image = objProducts.Image == "" ? "" : objProducts.Image;
            }
            newProduct.Name = objProducts.Name;
            newProduct.Price = objProducts.Price;
            newProduct.Image = objProducts.Image;
            newProduct.CREATED_DATE = DateTime.Now;
            newProduct.CREATED_BY_ID = 1;
            newProduct.UPDATED_DATE = DateTime.Now;
            newProduct.UPDATED_BY_ID = 1;
            objModel.Products.Add(newProduct);
            objModel.SaveChanges();
            return View("Index");
        }

------------------ end


Result :



Comments

Popular posts from this blog

Tree view in winforms using c#

how to Replace null value with 0 Using C#

how to fetch all HTML Table Records and modify all records accordingly using jquery and Javascript