在MVC中显示图像[VARBINARY(MAX)] [英] Display Image [VARBINARY(MAX)] in MVC
问题描述
我是MVC的新手.我将存储在表中的图片称为"Resim".
I'm new to MVC. I got pictures stored in my table called "Resim".
当我从视图中调用相关记录时,我想查看图片.我将使用其ID来调用它.这是我的控制器代码:
I want to see the pictures when I call the associated record from the view.I will use its id to call it. Here is my code of the controller:
public ActionResult Details(int id = 0)
{
SozlukEntities db = new SozlukEntities();
KelimeTuru kelime = db.KelimeTuru.Find(id);
if (kelime == null)
{
return HttpNotFound();
}
return View(kelime);
}
这是我的视图代码:
@model SozlukRG.Models.KelimeTuru
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<p>Move the mouse pointer over this paragraph.</p>
<legend>KelimeTuru</legend>
<div class="div1" style="display: table; background-color: #b0c4de;">
<div class="div1" style="display: table-row;">
<div class="div1" style="display: table-cell; padding: 10px;">
@Html.DisplayNameFor(model => model.KelimeId)
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
@Html.DisplayNameFor(model => model.VideoId)
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
@Html.DisplayNameFor(model => model.ResimId)
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
@Html.DisplayNameFor(model => model.Anlam)
</div>
</div>
<div class="div1" style="display: table-row;">
<div class="div1" style="display: table-cell; padding: 10px;">
@Html.DisplayFor(model => model.KelimeId)
<div class="div1" style="display: table-cell; padding: 10px;">
<video width="320" height="240" controls>
<source src="/Videos/b.mp4" type="video/mp4">
</video>
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
@*The Image will be inserted here, PLEASE HELP ME WITH THIS :) *@
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
@Html.DisplayFor(model => model.Anlam)
</div>
</div>
</div>
</fieldset>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
Resim
是表的名称,而 Adi
是带有图片的 VARBINARY(MAX)
列.我想在视图中查看图片.请帮助..谢谢...
Resim
is the name of the table and Adi
is the VARBINARY(MAX)
column with the picture. I want to see the picture in the view.
Please Help..Thanks in advance...
推荐答案
您可以创建一个专用的Image控制器来处理显示图像:
You could create a dedicated Image controller which handles displaying images:
public class ImageController : Controller
{
public ActionResult Show(int id)
{
SozlukEntities db = new SozlukEntities();
KelimeTuru kelime = db.KelimeTuru.Find(id);
byte[] data = kelime.Aidi;
return File(data, "image/jpg");
}
}
为了清晰起见,我跳过了错误检查.另外,返回类型可能不同于"image/jpg"
.
I skipped error checking for clarity. Also, the return type might be different than "image/jpg"
.
这篇关于在MVC中显示图像[VARBINARY(MAX)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!