1

I am trying to return a list of images as bytes to display on my view. I know how to return a single image by putting this in my controller

public FileContentResult DisplayImages(string packageID)

{

            byte[] byteArray = imageConverter.GetImageAsBytes(
                "\\filepath-to-image.jpeg");

            return new FileContentResult(byteArray, "image/jpeg"
}

How do I modify this to return a list of byte[] and then according call it in my View.

Thanks

1 Answer 1

1

I'll assume you'll call this action from javascript. In this case you could use the Json actionresult type to do this.

public ActionResult DisplayImages(string packageID)
{
  byte[] byteArray = imageConverter.GetImageAsBytes(@"\filepath-to-image.jpeg");
  return Json(new { imageList = new List<Byte[]>() { byteArray } });
}

you'll get an object with a imageList property which will be an Array of Array of Byte.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for replying. Yes I am calling the controller function via javascript. Will I have to specify the content-type in my view when calling?
I am currently just using <img src = @Html.ActionLink("Controller/Action") /> to render the image. Not sure how I would do this with a list of array bytes returned as json
You won't be able to do a list of images in a single request unless you do some sort of CSS sprite (combine it to a single image), make multiple requests, or do some binary manipulation with an HTML 5 canvas or something, so may want to explore a different route. The multiple request is probably the easiest.
Multiple requests is not really an option since I'm querying the database based on the packageID which returns 0 or many images
In that case it's a lot more difficult to get the thing done. As far as I know, you can't do this with just one request. You could break it up in 2 different requests, 1 to fetch the binaries from the database into the user session / webserver cache and returns an identifier and the number of images fetched and another which will take the identifier and a number as parameter and outputs the corresponding image data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.