【问题标题】:Fetching images in byte data from database in asp.net mvc 5从asp.net mvc 5中的数据库中获取字节数据中的图像
【发布时间】:2017-05-04 06:36:49
【问题描述】:

我正在尝试以二进制格式显示存储在 sql 数据库中的图像。我正在使用剃刀语法通过将其格式更改为 base64 来检索图像。字节数据被成功检索,但从未显示为图片格式。以下是我迄今为止尝试过的代码。谢谢!

酒店信息

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

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

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>

【问题讨论】:

  • 你得到你的base64字符串了吗?如果您这样做,则与 mvc 或剃刀无关。这是关于html的。尝试通过其他一些 base64 让我们看看它是否有效。如果是这样,那么您的 base64 不是图像
  • 如果它的 base64 确保它有前导和/或尾随空格,否则它将无法工作。
  • 尝试在...src="@imagesrc"....前面加一个@(at)符号
  • @Webbanditten,谢谢哥们!这只是一个要添加的@符号,所有图片都显示出来了:)

标签: c# asp.net razor asp.net-mvc-5


【解决方案1】:

使用 Razor C# 语法 - 内联表达式(变量和函数)以 @ 开头。

所以如果你编辑

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

&lt;img src = "@imagesrc" style = 'max-height:100px;max-width:100px' /&gt;

您的代码应该可以成功运行。

【讨论】:

  • 完全正确。我没有意识到他错过了,有趣:)
猜你喜欢
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多