0

I haven't been able to get this to work using answers to other similar questions, maybe this case is different.

I'm trying to turn a byte array into an image using asp.net and mvc.

Here's my view:

<div class="carousel-item">
    <img class="d-block w-100" src="@Url.Action( "Show", "Home", new { id = i.ProductID })" alt="@i.ProductName">
</div>

Here's the Show method in my controller:

    public ActionResult Show(int id)
    {
        IQueryable<Product> products = from p in db.Products
                                       where id == p.ProductID
                                       select p;

        var imageData = products.First().Picture;

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

If anybody could clue me in to what I'm doing incorrectly here, that would be great.

Edit I have tried turning the byte array into a base64 string as the answers of the "possibly duplicate" question directed to do, however that did not render as an image.

Kyle Diablo
  • 61
  • 1
  • 7
  • @NirmalSubedi byte[] – Kyle Diablo Dec 07 '18 at 18:57
  • You want to convert the image into a base64 string. https://stackoverflow.com/q/17952514/2030565. – Jasen Dec 07 '18 at 18:57
  • It only needs to be base64 encoded if the OP is requesting the file in an AJAX request. MVC actions can return byte streams containing an image, unencoded, and usable from image tags. –  Dec 07 '18 at 19:00
  • Those answers from the other link didn't actually help. – Kyle Diablo Dec 07 '18 at 19:05
  • test it as follows: Put http://site/Home/Show/ in a browser address bar, then use a tool like Chrome dev tools to see if you actually got a valid JPG file in the response. My suspicion is that the original data is not truly binary or is not a valid JPG. – Mike Marshall Dec 07 '18 at 19:19
  • @MikeMarshall Doing that yielded a blank screen. here is what is stored in the database for the picture column for record 1: 0x433A5C55736572735C446961626C6F5C50696374757265735C57656273697465506963735C737061646F6E612E6A7067 – Kyle Diablo Dec 07 '18 at 19:29

1 Answers1

0

my asp.net mvc project was having an issue with the way my images were stored in the database. They were stored as the image datatype, but changing that to varbinary(MAX) and updating each record with a new image path solved the issue.

Kyle Diablo
  • 61
  • 1
  • 7