在MVC中显示图像[VARBINARY(MAX)]

时间:2014-04-07 15:19:08

标签: asp.net-mvc asp.net-mvc-4

我是MVC的新手。我把照片存放在我的桌子上,名为" Resim"。 enter image description here

我想从视图中调用相关记录时看到图片。我将使用其ID来调用它。这是我的控制器代码:

    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)列。我想在视图中看到图片。 请帮助..提前谢谢......

1 个答案:

答案 0 :(得分:2)

您可以创建一个处理显示图像的专用图像控制器:

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"不同。