简体   繁体   English

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

[英]Show images stored in mysql database + ASP.NET MVC

I want to show the images stored in my database in my View. 我想在视图中显示存储在数据库中的图像。
I have a field "deliverable_image" from the type "longblob" in my mysql database. 我的MySQL数据库中有一个类型为“ longblob”的字段“ deliverable_image”。

In my controller: 在我的控制器中:

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

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

Repository: 仓库:

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

View: 视图:

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

But in my controller I get the error: 但是在我的控制器中,我得到了错误:

cannot convert from 'System.Linq.IQueryable' to 'string' 无法从“ System.Linq.IQueryable”转换为“字符串”

Does anybody knows how I can fix this? 有人知道我该如何解决吗?

In your action method, the very first line gets a collection of images (specifically, an IQueryable<byte[]> : 在您的操作方法中,第一行获取图像集合(特别是IQueryable<byte[]>

var imageData = repository.GetAllImages();

Then you try to send all of the images as a single file: 然后,您尝试将所有图像作为一个文件发送:

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

imageData is a collection of files. imageData是文件的集合。 You need to select one of them and return just that. 您需要选择其中之一并返回。 To do that, you'll probably need to modify your GetAllImages function or use a different function. 为此,您可能需要修改GetAllImages函数或使用其他函数。 The action method is being passed an id , but you can't use that on the results of that function because all you have are byte arrays. 正在向该操作方法传递一个id ,但是您不能在该函数的结果上使用该方法,因为您所拥有的只是字节数组。 You need to filter for the id specified and then use the resulting byte array. 您需要过滤指定的id ,然后使用结果字节数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM