Monday, 29 June 2015

Image file upload code using MVC 4?


Hi Guys,
 This blog we explain how to save upload image file within database and show it.
Sanimage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace sanaprilmvc.Models
{
    public class samimagefile
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string ImageFile { get; set; }
    }
}
Sanimagefileuploadcontroller.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sanaprilmvc.Models;

namespace sanaprilmvc.Controllers
{
    public class sanimagefileuploadController : Controller
    {
        //
        // GET: /sanimagefileupload/
        sanimagepromvcEntities sanimg = new sanimagepromvcEntities();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult FileUpload(HttpPostedFileBase file)
        {
            try
            {
                if (file != null)
                {
                    string imagename = System.IO.Path.GetFileName(file.FileName);
                    string physicalpath = Server.MapPath("/Images/" + imagename);
                    file.SaveAs(physicalpath);
                    sanimage newimage = new sanimage();
                    newimage.Name = Request.Form["Name"];
                    newimage.Description = Request.Form["Description"];
                    newimage.ImageFile = imagename;
                    sanimg.sanimages.Add(newimage);
                    sanimg.SaveChanges();
                    return RedirectToAction("Display");
                    ViewBag.msg = "Successfully upload file";
                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "Error on file";
            }
            return View();
        }
        public ActionResult Display()
        {
            return View();
        }
}
}
fileupload.cshtml
@model sanaprilmvc.Models.samimagefile

@{
    ViewBag.Title = "FileUpload";
}

<h2>FileUpload</h2>
@using (Html.BeginForm("FileUpload", "sanimagefileupload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
 <div>
     Name<br />
     @Html.TextBox("Name")<br />
     Description<br />
     @Html.TextBox("Description")<br />
     Image<br />
     <input type="file" name="file" id="file" /><br />
     <input type="submit" value="Upload" />
     <p>@ViewBag.msg</p>
     <p style="color:blue">@Html.ActionLink("Go to Display", "Display" ,"sanimagefileupload")</p>
   
 </div>
}
Display.cshtml
@{
    ViewBag.Title = "Display";
}

<h2>Display</h2>
@{
    sanaprilmvc.sanimagepromvcEntities sanimg = new sanaprilmvc.sanimagepromvcEntities();
}
@using (Html.BeginForm())
{
<table>
    <thead>
        <tr>
            <th>
                Image
            </th>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in sanimg.sanimages)
        {
        <tr>
            <td><img src="/Images/@item.ImageFile" width="100" height="100" /></td>
            <td>@item.Name</td>
            <td>@item.Description</td>
        </tr>
        }
    </tbody>
</table>
}

No comments:

Post a Comment