Problem with displaying image from DB using asp.net MVC 2

Posted by Renato on Stack Overflow See other posts from Stack Overflow or by Renato
Published on 2010-03-15T15:12:36Z Indexed on 2010/03/15 15:29 UTC
Read the original article Hit count: 236

Filed under:
|
|
|

Hello! I'm having trouble with displaying image from db. I think that method for saving image is working, because I see in DB that varbinary fields have some value, and that image type is correct. Also the size of image. But when I want to display image for product i don't get anything. Just blank space.. here is my code...

public byte[] GetImage(int productID)
    {
        Product product = products.GetByID(productID);

        byte[] imageData = product.ImageData.ToArray();

        return imageData;
    }

That code is in my controller. Second is code from view :

<% if (product.ImageData != null) { %>
    <img src='/product/getimage/<%= Html.Encode(product.ID) %>' alt=""/>
    <% } %>

I tried some solutions found here on stack overflow, and everyone do it like this, but it's working for them. I dont have any idea why images aren't displayed. When i look at source code of page at debugging i have :

<img src='/product/getimage/18' alt=""/>

I'm using .net 4.0, MVC 2, VS 2010... Thanks in advance

© Stack Overflow or respective owner

Related posts about image

Related posts about mvc