asp.net - 显示存储在 mysql 数据库中的图像 + ASP.NET MVC

标签 asp.net mysql asp.net-mvc image

我想在我的 View 中显示存储在数据库中的图像。
我的 mysql 数据库中有一个类型为“longblob”的字段“deliverable_image”。

在我的 Controller 中:

public ActionResult Show( int id )
    {
        var imageData = repository.GetAllImages();

        return File( imageData, "image/jpg" );
    }

存储库:

public IQueryable<byte[]> GetAllImages()
    {
        return from deliverable in entities.deliverables
               orderby deliverable.deliverable_image
               select deliverable.deliverable_image;
    }

查看:

<img src='<%= Url.Action( "Show", "Deliverable", new { id = ViewData["DeliverableID"] } ) %>' />

但是在我的 Controller 中我收到错误:

cannot convert from 'System.Linq.IQueryable' to 'string'

有人知道我该如何解决这个问题吗?

最佳答案

在您的操作方法中,第一行获取图像集合(具体来说,IQueryable<byte[]>:

var imageData = repository.GetAllImages();

然后您尝试将所有图像作为单个文件发送:

return File(imageData, "image/jpg");

imageData是文件的集合。您需要选择其中之一并返回。为此,您可能需要修改 GetAllImages函数或使用不同的函数。正在向操作方法传递 id ,但您不能在该函数的结果上使用它,因为您拥有的只是字节数组。您需要过滤 id指定,然后使用生成的字节数组。

关于asp.net - 显示存储在 mysql 数据库中的图像 + ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16903834/

相关文章:

c# - bootStrap 菜单添加的元素无法访问后面的 asp.net 代码

asp.net - 在 IHttpAsyncHandler 中使用 Task 或 async/await

java - 如何在具有不同行的多对一关系字典表中?

asp.net-mvc - 在没有 UI 自动化的痛苦的情况下集成测试 MVC 应用程序

javascript - xmlhttprequest 无法仅使用 post 方法加载 'access-control-allow-origin' (Angularjs)

jquery - 为什么我的部分 View 在页面加载时验证?

c# - 为网站使用静态 ADO.NET 函数不好吗?

Mysql - 移动和替换列

mysql - 尝试导入 CSV 文件时数据被截断

asp.net-mvc - 具有长期有效期的 OpenId ASP MVC 身份验证