I want to retrieve Image from database and display it same controller 'Index' view or other controller 'Index' view. I think already I successfully insert Image data in database. Here is my code..Can anybody help me that what action/method and 'Html' form now I should write in my code..
Model class Picture
:
public class Picture
{
public int PictureId { get; set; }
public string Name { get; set; }
public static byte[] Image { get; set; }
}
PictureController
:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(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.Image = Image;
}
if (ModelState.IsValid)
{
db.Pictures.Add(picture);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(picture);
}
Create
view of PictureController
@model PartialView.Models.Picture
@{ ViewBag.Title = "Create"; }
<h2>Create</h2>
@using (Html.BeginForm(null, null, FormMethod.Post,
new { enctype = "multipart/form- data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Picture</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<td>File :</td>
<td><input type="file" name="Image" id="Image" /> </td>
<p>
<input type="submit" value="Create" />
</p>
</fieldset> }
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Database table after insert of two data rows:
PictureId Name
-------------------------
1 Song
2 Clothes
See Question&Answers more detail:os