88

Anyone know of any step by step tutorials on how to upload/display images from a database using Entity Framework? I've checked out code snippets, but I'm still not clear on how it works. I have no code, because aside from writing an upload form, I'm lost. Any (and I mean any) help is greatly appreciated.

On a sidenote, why don't any books cover this topic? I have both Pro ASP.NET MVC 4 and Professional MVC4, and they make no mention of it.

4
  • 6
    If you follow Pro ASP MVC 4 the SportsStore Tutorial does cover this starting on page 292
    – Komengem
    Apr 27, 2013 at 20:24
  • you're right. I didn't even notice. Was looking for a chapter on it. Thanks
    – rogerthat
    Apr 27, 2013 at 20:38
  • 2
    I know this is for MVC 4, but this question will still show up when looking for MVC 5. - I found a great tutorial which covers both Upload to a Database as Upload to a file server in Mvc 5 using EF 6 on MikesDotNetting mikesdotnetting.com/article/259/…
    – Vahx
    Apr 24, 2016 at 12:40
  • The chapter in the book covers uploading images to the database, where most people want to save the path to the database and the image to a folder.
    – whisk
    Oct 26, 2016 at 14:04

3 Answers 3

144

Have a look at the following

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, 
                            new { enctype = "multipart/form-data" }))
{  
    <label for="file">Upload Image:</label> 
    <input type="file" name="file" id="file" style="width: 100%;" /> 
    <input type="submit" value="Upload" class="submit" /> 
}

your controller should have action method which would accept HttpPostedFileBase;

 public ActionResult FileUpload(HttpPostedFileBase file)
    {
        if (file != null)
        {
            string pic = System.IO.Path.GetFileName(file.FileName);
            string path = System.IO.Path.Combine(
                                   Server.MapPath("~/images/profile"), pic); 
            // file is uploaded
            file.SaveAs(path);

            // save the image path path to the database or you can send image 
            // directly to database
            // in-case if you want to store byte[] ie. for DB
            using (MemoryStream ms = new MemoryStream()) 
            {
                 file.InputStream.CopyTo(ms);
                 byte[] array = ms.GetBuffer();
            }

        }
        // after successfully uploading redirect the user
        return RedirectToAction("actionname", "controller name");
    }

Update 1

In case you want to upload files using jQuery with asynchornously, then try this article.

the code to handle the server side (for multiple upload) is;

 try
    {
        HttpFileCollection hfc = HttpContext.Current.Request.Files;
        string path = "/content/files/contact/";

        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            if (hpf.ContentLength > 0)
            {
                string fileName = "";
                if (Request.Browser.Browser == "IE")
                {
                    fileName = Path.GetFileName(hpf.FileName);
                }
                else
                {
                    fileName = hpf.FileName;
                }
                string fullPathWithFileName = path + fileName;
                hpf.SaveAs(Server.MapPath(fullPathWithFileName));
            }
        }

    }
    catch (Exception ex)
    {
        throw ex;
    }

this control also return image name (in a javascript call back) which then you can use it to display image in the DOM.

UPDATE 2

Alternatively, you can try Async File Uploads in MVC 4.

7
  • wow. Thanks. I'll try it out. Any resources, that you can think of, I can read to further my knowledge on this subject? I can't find anything.
    – rogerthat
    Apr 27, 2013 at 19:40
  • 1
    after doing file.SaveAs(path) how can I then delete the file from that path?
    – J86
    Mar 24, 2015 at 14:49
  • Using angularjs to send html file type image to server side is more easy , stackoverflow.com/a/26409100/900284 Dec 29, 2015 at 14:57
  • @FrankMyatThu using angular only for uploading images ? doesn't that sound a little bit odd ? Mar 8, 2016 at 13:21
  • 1
    @DotNetDreamer I'd check your website out ASAP as there's detailed error logging on there exposing some rather important information at the moment...
    – Gareth
    Apr 12, 2019 at 13:56
46

Here is a short tutorial:

Model:

namespace ImageUploadApp.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Image
    {
        public int ID { get; set; }
        public string ImagePath { get; set; }
    }
}

View:

  1. Create:

    @model ImageUploadApp.Models.Image
    @{
        ViewBag.Title = "Create";
    }
    <h2>Create</h2>
    @using (Html.BeginForm("Create", "Image", null, FormMethod.Post, 
                                  new { enctype = "multipart/form-data" })) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Image</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.ImagePath)
            </div>
            <div class="editor-field">
                <input id="ImagePath" title="Upload a product image" 
                                      type="file" name="file" />
            </div>
            <p><input type="submit" value="Create" /></p>
        </fieldset>
    }
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    
  2. Index (for display):

    @model IEnumerable<ImageUploadApp.Models.Image>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ImagePath)
            </th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ImagePath)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Ajax.ActionLink("Delete", "Delete", new {id = item.ID} })
            </td>
        </tr>
    }
    
    </table>
    
  3. Controller (Create)

    public ActionResult Create(Image img, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            if (file != null)
            {
                file.SaveAs(HttpContext.Server.MapPath("~/Images/") 
                                                      + file.FileName);
                img.ImagePath = file.FileName;
            }  
            db.Image.Add(img);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(img);
    }
    

Hope this will help :)

8
  • Wow, thank you so much for this. What if I wanted to restrict the image to jpeg?
    – Kala J
    Oct 21, 2013 at 14:47
  • @KalaJ If you want to restrict the images uploaded to only jpg you can add the accept attribute (Html5 only working in Chrome and FF not IE). Or you can check the extension in controller filename.LastIndexOf(".") something like that. Hope this helps Oct 29, 2013 at 17:35
  • @JordyvanEijk, I used the accept attribute however it only works in Chrome. It doesn't work in FF or IE. I need some other form of validation.
    – Kala J
    Nov 7, 2013 at 12:43
  • 1
    @KalaJ Majbe you can do something with the Html5 File Api If you need some tutorial Here it is Nov 7, 2013 at 16:32
  • 2
    Hackers would be able to upload malicious files without server-side validation.
    – arao6
    Sep 21, 2014 at 14:22
-16
        <input type="file" id="picfile" name="picf" />
       <input type="text" id="txtName" style="width: 144px;" />
 $("#btncatsave").click(function () {
var Name = $("#txtName").val();
var formData = new FormData();
var totalFiles = document.getElementById("picfile").files.length;

                    var file = document.getElementById("picfile").files[0];
                    formData.append("FileUpload", file);
                    formData.append("Name", Name);

$.ajax({
                    type: "POST",
                    url: '/Category_Subcategory/Save_Category',
                    data: formData,
                    dataType: 'json',
                    contentType: false,
                    processData: false,
                    success: function (msg) {

                                 alert(msg);

                    },
                    error: function (error) {
                        alert("errror");
                    }
                });

});

 [HttpPost]
    public ActionResult Save_Category()
    {
      string Name=Request.Form[1]; 
      if (Request.Files.Count > 0)
        {
            HttpPostedFileBase file = Request.Files[0];
         }


    }
2
  • So, how would you display, "msg" as an image to img?
    – eaglei22
    May 17, 2017 at 21:13
  • You should delete this answer and get your Reputation points back.
    – Rohan Rao
    Nov 18, 2019 at 4:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.