如何从数据库映像 [英] How to Retrieve Image from database
问题描述
我想从数据库中检索并显示图像相同的控制器索引视图或其他控制器索引视图。我想我已经成功地插入数据库中的图像数据。这里是我的code..Can人帮助我,什么样的行动/法和HTML格式,现在我应该在code写的..
Model类图片
:
公共类图片
{
公众诠释PictureId {搞定;组; }
公共字符串名称{;组; }
公共静态的byte []图片{搞定;组; }
}
PictureController
:
[HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult创建(画中画)
{
字节[]图片;
如果(Request.Files [文件]!= NULL)
{
使用(VAR binaryReader =新BinaryReader(Request.Files [文件]的InputStream))
{
图像= binaryReader.ReadBytes(Request.Files [文件] CONTENTLENGTH);
}
Picture.Image =图像;
}
如果(ModelState.IsValid)
{
db.Pictures.Add(图片);
db.SaveChanges();
返回RedirectToAction(「指数」);
} 返回查看(图)
}
创建
视图 PictureController
@model PartialView.Models.Picture @ {ViewBag.Title =创建; } < H2>创建< / H> @using(Html.BeginForm(NULL,NULL,FormMethod.Post,
新{ENCTYPE =多部分/形式 - 数据}))
{
@ Html.AntiForgeryToken()
@ Html.ValidationSummary(真)<&字段集GT;
<传奇>图片和LT; /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Name)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Name)
@ Html.ValidationMessageFor(型号=> model.Name)
< / DIV>
< TD>文件:LT; / TD>
< TD><输入类型=文件名称=图像ID =图片/> < / TD> &所述p为H.;
<输入类型=提交值=创建/>
&所述; / P>
< /字段集> }
< DIV>
@ Html.ActionLink(返回目录,索引)
< / DIV>@section脚本{@ Scripts.Render(〜/包/ jqueryval)}
数据库表的两个数据行插入后:
PictureId名称
-------------------------
1首歌曲
2服
为了在您需要定义有filecontentresult的返回类型的控制器操作的用户界面来显示图像。下面是一个例子:
公共FileContentResult GETIMG(INT ID)
{
字节[]的字节数组= DbContext.Persons.Find(ID),在图像配;
如果(的字节数组!= NULL)
{
返回新FileContentResult(字节数组,图像/ JPEG);
}
其他
{
返回null;
}
}
一旦你定义控制器动作,那么你可以用code的下面一行显示的字节[]作为在视图中的图像。
< IMG SRC =@ Url.Action(GETIMG,人,新{ID = item.Id})ALT =人物形象/>
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
In order to display image in the UI you need to define a controller action which has a return type of filecontentresult. following is an example :
public FileContentResult getImg(int id)
{
byte[] byteArray = DbContext.Persons.Find(id).Image;
if (byteArray != null)
{
return new FileContentResult(byteArray, "image/jpeg");
}
else
{
return null;
}
}
Once you have defined the controller action then you can use following line of code to show the byte[] as an image in the view.
<img src="@Url.Action("getImg", "Person", new { id = item.Id })" alt="Person Image" />
这篇关于如何从数据库映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!