【问题标题】:Uploading and displaying multiple images from database in MVC在 MVC 中从数据库上传和显示多个图像
【发布时间】:2016-01-04 14:35:57
【问题描述】:

我正在使用 ASP.NET MVC,我正在尝试将多个图像上传到数据库,然后在视图中显示它们。我相信上传过程正在运行,但是当我尝试显示图像时,仅显示替代文本并且在 Chrome 调试中找不到资源错误。

这就是我的图像在我的视图中的显示方式

@model IList<InspectionPhoto>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="myModalLabel">Photo Management</h4>
</div>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="modal-body">
        <div class="form-horizontal">

            @if (Model.Count > 0)
            {
                <div class="row">
                    @for (var i = 0; i < Model.Count(); i++)
                    {
                        <div class="row">
                            <div class="col-md-6">
                                <img src="@Url.Action( "ShowPhoto", "Inspection", new { id = Model[i].PhotoID } )" alt="Image not available" />
                            </div> 
 //continues

显示照片方法:

public Bitmap ShowPhoto(int id)
{
    InspectionPhoto image = db.InspectionPhotoes.Find(id);
    var result = (Bitmap)((new ImageConverter()).ConvertFrom(image.ImageBytes));
    return result;
}

show photo 方法在我的 Inspections 控制器中,因为 Photo 视图是一个模态局部视图,当在检查视图中单击按钮时会显示该视图。

任何帮助将不胜感激。如果需要更多信息,请告诉我。

【问题讨论】:

  • 1) 您是否在控制器中设置了断点? 2)控制器的名字是Inspection还是Inspections? 3) 在您看来,您期望的是一个 URL,但您的操作方法返回一个位图...

标签: c# asp.net-mvc image-uploading


【解决方案1】:

ShowPhoto 返回 Bitmap 但应该返回 File 如下所示。如果您的图像保存正确,那么以下应该可以正常工作。

public ActionResult ShowPhoto(int id)
{
    InspectionPhoto image = db.InspectionPhotoes.Find(id);
    byte[] result = image.ImageBytes;
    return File(result, "image/png");
}

【讨论】:

    【解决方案2】:

    您的ShowPhoto 方法返回一个位图,您尝试写入的是html 的img src 标记,它需要一个url 字符串。您应该修改 ShowPhoto 函数以返回与已上传的图像关联的 url。

    【讨论】:

      【解决方案3】:

      我认为你应该返回 FileContent

          public FileContentResult GetImage(int id)
          {
              InspectionPhoto image = db.InspectionPhotoes.Find(id);
              byte[] byteArray = image.ImageBytes ;
              return byteArray != null
                  ? new FileContentResult(byteArray, "image/jpeg")
                  : null;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-07
        • 2017-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        相关资源
        最近更新 更多