简体   繁体   English

在ASP.NET MVC 5中从数据库中获取字节数据中的图像

[英]Fetching images in byte data from database in asp.net mvc 5

I'm trying to display images stored in sql database in binary format. 我正在尝试以二进制格式显示存储在sql数据库中的图像。 I'm using razor syntax to retrieve images by changing its format to base64. 我正在使用剃刀语法通过将其格式更改为base64来检索图像。 The byte data is successfully retrieved, but never displayed as a picture format. 字节数据已成功检索,但从未以图片格式显示。 Below is the code that I've tried so far. 以下是到目前为止我尝试过的代码。 Thanks! 谢谢!

HotelInfo 酒店信息

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Travel.Context;

namespace Travel.Models.Hotel
{
    public class HotelInfo
    {
        private int _hotelid;
        private string _hotelname;
        private string _hoteldesc;
        private string _hotelprice;
        private byte[] _hotelpicture;
        //private HttpPostedFileBase _UploadedFile;



        public HotelInfo()
        {
            this._hotelid = 0;
            this._hotelname = string.Empty;
            this._hoteldesc = string.Empty;
            this._hotelprice = string.Empty;

        }

        [Key]
        public int Hotelid
        {
            get
            {
                return _hotelid;
            }

            set
            {
                _hotelid = value;
            }
        }

        public string Hotelname
        {
            get
            {
                return _hotelname;
            }

            set
            {
                _hotelname = value;
            }
        }

        public string Hoteldesc
        {
            get
            {
                return _hoteldesc;
            }

            set
            {
                _hoteldesc = value;
            }
        }

        public string Hotelprice
        {
            get
            {
                return _hotelprice;
            }

            set
            {
                _hotelprice = value;
            }
        }


        public byte[]  Hotelpicture
        {
            get
            {
                return _hotelpicture;
            }

            set
            {
                _hotelpicture = value;
            }
        }


    }
}

HotelController.cs HotelController.cs

public ActionResult HotelDescription()
        {
            return View(db.Hotels.ToList());
        }

HotelDescription.cshtml HotelDescription.cshtml

@model IEnumerable<Travel.Models.Hotel.HotelInfo>

@{
    ViewBag.Title = "HotelDescription";
    Layout = "~/Views/Shared/_Header.cshtml";
}

<h2>HotelDescription</h2>

<p>
    @Html.ActionLink("Create New", "CreateHotel")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Hotelname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Hoteldesc)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Hotelprice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Hotelpicture)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Hotelname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Hoteldesc)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Hotelprice)
        </td>
        <td>
            @{
                var base64 = Convert.ToBase64String(item.Hotelpicture);
                var imagesrc = string.Format("data:image/jpeg;base64,{0}", base64);

            }
           <img src = "imagesrc" style = 'max-height:100px;max-width:100px' />

        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Hotelid }) |
            @Html.ActionLink("Details", "Details", new { id=item.Hotelid }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Hotelid })
        </td>
    </tr>
}

    @section Scripts {

    }

</table>

Using Razor C# Syntax - Inline expressions (variables and functions) start with @. 使用Razor C#语法-内联表达式(变量和函数)以@开头。

Therefore if you edit 因此,如果您编辑

<img src = "imagesrc" style = 'max-height:100px;max-width:100px' />

To

<img src = "@imagesrc" style = 'max-height:100px;max-width:100px' />

Your code should work successfully. 您的代码应该可以成功运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM