nielsv
nielsv

Reputation: 6800

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.

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'

Does anybody knows how I can fix this?

Upvotes: 0

Views: 660

Answers (1)

David
David

Reputation: 218798

In your action method, the very first line gets a collection of images (specifically, an 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. 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. 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. You need to filter for the id specified and then use the resulting byte array.

Upvotes: 1

Related Questions