Reputation: 648
I am a beginner in MVC and am trying to add an image to the database and then retrieve it on the front end.
I have added my class:
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
namespace TestProject.Data
{
public partial class UpcomingEvent
{
public UpcomingEvent()
{
EventDate = DateTime.Now;
DateTimeStamp = DateTime.Now;
Cancelled = false;
}
[Key]
public int EventID { get; set; }
public DateTime EventDate { get; set; }
public string Title { get; set; }
public string Blurb { get; set; }
public string Body { get; set; }
public byte[] EventImage { get; set; }
public DateTime DateTimeStamp { get; set; }
public bool Cancelled { get; set; }
}
}
And then created a controller with read/write actions using Entity Framework. In the create / edit etc all the fields display other than the image.
What do I need to add to my controller or view in order to send an image to the DB with each entry?
Upvotes: 4
Views: 7472
Reputation: 2611
Here is a working example from one of our websites for insurance agencies. I am storing the logo in a directory. I am also resizing the logo, and creating a mobile version using ImageResizer (can be installed with Package Manager). While that's not necessary, it is a nice tool to have, especially when accepting uploads from end users.
View:
@using (Html.BeginForm("Upload","LogoController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-horizontal">
<div class="control-group">
<label>
Upload A Logo</label>
<div class="controls">
<input type="file" name="logo" />
</div>
</div>
</div>
<p><input type="submit" name="Upload" value="Upload" class="button" />
</p>
}
Controller:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase logo)
{
SaveLogos(logo);
return View();
}
private void SaveLogos(HttpPostedFileBase logo)
{
if (logo != null && logo.ContentLength > 0)
{
var ext = Path.GetExtension(logo.FileName);
var path = Server.MapPath("~/Content/Images");
var full = Path.Combine(path, "logo.png");
path = Path.Combine(path, "Mobile");
var mobile = Path.Combine(path, "logo.png");
var tmp = Path.GetTempFileName() + "." + ext;
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (System.IO.File.Exists(full))
{
System.IO.File.Move(full, full.Replace(".png", DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".png"));
}
if (System.IO.File.Exists(mobile))
{
System.IO.File.Move(mobile, mobile.Replace(".png", DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".png"));
}
// convert to png
logo.SaveAs(tmp);
var job = new ImageResizer.ImageJob(tmp, full,
new ImageResizer.ResizeSettings("width=460;height=102;format=png;mode=pad"));
job.Build();
// create mobile image
job = new ImageResizer.ImageJob(tmp, mobile,
new ImageResizer.ResizeSettings("width=190;height=44;format=png;mode=pad"));
job.Build();
}
catch (Exception e)
{
Logging.LogError(e, ControllerContext);
}
finally
{
System.IO.File.Delete(tmp);
}
}
}
Storing this into EF would be a matter of obtaining the bytes for the images, possibly encoding them (base64 perhaps), and then storing the result into your object. This CodeProject Article provides examples of storing files into a varbinary(MAX) field.
Lastly, I would be negligent if I didn't offer a caveat about storing images in your database. Microsoft recommends against this if the images are larger than 1MB. See Entity Framework Column Type for Base64 String for a similar discussion.
Upvotes: 4
Reputation: 157
You can search on the Internet first.
Look at this page: http://www.mikesdotnetting.com/article/125/asp-net-mvc-uploading-and-downloading-files
Upvotes: -2