Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return multiple images from controller method?

I have an ASP.NET MVC controller that generates images (they are stored in memory, I don't want to store them on the hard drive) and should return them to my view.

The problem is: I don't how know how to return multiple images from one controller method (I want to show the images in 1 view). I know that I can return a single image with the FileResult for example, but I can't find out (not on google/stackoverflow) how to return multiple images from the same method. Splitting the method up in multiple methods can't be done. Oh, all of the images are converted to a byte[], but that can be reversed if necessary.

like image 426
Leon Cullens Avatar asked Sep 06 '25 22:09

Leon Cullens


1 Answers

This should work. Note I am reading my images from disk for my example but they can come from memory or anywhere. Then on the client side use java-script to display them.

[HttpGet]
public JsonResult Images()
{
    var image1Base64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(Server.MapPath("~/Images/1.jpg")));
    var image2Base64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(Server.MapPath("~/Images/2.jpg")));

    var jsonResult = Json(new { image1 = image1Base64, image2 = image2Base64 }, JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;

    return jsonResult;
}
like image 123
lahsrah Avatar answered Sep 09 '25 18:09

lahsrah