我想在我的 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/