【问题标题】:display images from the database显示来自数据库的图像
【发布时间】:2013-05-08 04:13:15
【问题描述】:

我需要显示数据库中的图像。 在控制器中:

public ActionResult Display(int id, Document doc)
{
    byte[] byteArray = doc.Content;//its has the image in bytes
    return new FileStreamResult(new System.IO.MemoryStream(byteArray), "image/jpeg");
}

在视图中:

@foreach (var imgsrc in Model.ImagesSrc)
{ 
     <img src="@Url.Action( "Display", "image", new { id = @imgsrc.Id } )" alt="" />
}

它不工作

【问题讨论】:

  • “它不工作”不足以描述正在发生的事情。请阅读tinyurl.com/so-hints
  • 在输出中显示带有问号的图像
  • 那么您是否尝试下载正在提供的文件?用 Wireshark 查看网络流量?您尝试过哪种诊断方法?
  • 查看源码中,image标签的格式如下 无论是否正确
  • 好吧,一个图片 URL 不包含扩展名肯定是不寻常,但是如果您尝试获取该 URL 会发生什么?它会下载任何东西吗?基本上你需要自己做一些诊断工作......

标签: c# asp.net-mvc razor


【解决方案1】:

您的控制器方法需要 Document doc,但您的 url 只定义了 id。您应该在下载方法中使用 id 获取文档

public ActionResult Display(int id)
{
    Document doc = GetDocById(id);
    byte[] byteArray = doc.Content;//its has the image in bytes
    return new FileStreamResult(new System.IO.MemoryStream(byteArray), "image/jpeg");
}

【讨论】:

    猜你喜欢
    • 2013-05-19
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    相关资源
    最近更新 更多