0

here is my view

     @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table> 
    <tr>

<td>File :</td>    
    <td><input type="file" name="File" id="file" /> </td>
  </tr>
  <tr> 
  <td><input type="submit" name="submit" value="upload" /></td>
  </tr>

Here is my Controller

public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(Picture picture)
        {
            if (picture.File.ContentLength > 0)
            {
                var fileName = Path.GetFileName(picture.File.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
                picture.File.SaveAs(path);
            }
            return RedirectToAction("Index");
        }


and Model:

namespace FileUpload.Models
{
    public class Picture
    {
        public HttpPostedFileBase File { get; set; }



    }

This code helps me to save image in my MVC project root Image folder , but I want to save it to my database . I have tried many tutorial but could not succeed yet ... ' I am Actually making the student form every student will register his picture.

1 Answer 1

1

Convert your image into bytes and then store it in your database

[HttpPost]
        public ActionResult Index(Picture picture)
        {
            byte[] Image;
            if (Request.Files["files"] != null)
            {
               using (var binaryReader = new BinaryReader(Request.Files["file"].InputStream))
                {
                   Image = binaryReader.ReadBytes(Request.Files["files"].ContentLength);
                }
               Picture.File =Image;
            }
            return RedirectToAction("Index");
        }

Model

public class Picture
    {
        public byte[] File { get; set; }
    }

View For Displaying Image

if (Model.File != null)
  {
     string imageBase64 = Convert.ToBase64String(Model.File );
     string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
     <img src="@imageSrc" width="100" height="100" />
  }
Sign up to request clarification or add additional context in comments.

7 Comments

Could you please give me a solution that how can I retrieve those Image from database and show in 'Index' view..
Is not those code I should be write in 'View' model ?? In my View model compiler does not resolve '@imageSrc' word and another query, when I write your upload method that time compiler automatic convert 'Picture Model' byte[] as static....why this happens ??
@mgsdew actually i am not getting it...can you post your code
My partner already post this problem in Stack Overflow..kindly check the link stackoverflow.com/questions/23590604/…
@mgsdew show me the code for retrieving image from db which you have done and remove static keyword from model static byte[] Image
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.