0

When a row in the HTML table is clicked , the ajax call is made to the MVC3 Controller that returns a json object with a byte array of the image , but Empty image getting displayed in the view. This should work from IE 7 TO 9

The controller code is :

    [HttpPost]
    public ActionResult RenderImage(string code)
    {
        ImageVM viewmodel = GetImage(code)
        return Json(viewmodel.Chart, "image/png");
     }

The javascript code for raising the ajax call and display the image is

  $(document).ready(function () {
      $('#Table tr').click(function (event) { 
      var id= $(this).attr('id')
         $.post("/Gateway/RenderImage", { "code": id },
     function (data) {
                alert(data);
            $('#ChartDiv').html('<img height="200" width="250" src="data:image/png;base64,' + data + '" />');

            });
       });
  });

1 Answer 1

1

I changed your code a bit and this is working with me :)

 [HttpPost]
        public virtual JsonResult RenderImage(string code)
        {
//for test data Im using the below, you can use yours :   GetImage(code)        
var data =   
  @"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC";
            return Json(data);
        }

and in the Javascript (removed one backslash)

 $('#Table tr').click(function (event) {
        var id = $(this).attr('id')
        $.post("Gateway/RenderImage", { "code": id },
    function (data) {
        alert(data);
        $('#ChartDiv').html('<img height="200" width="250" src="data:image/png;base64,' + data + '" />');

    });
    });

[Edit] to make the above code work with a png file we can change the controller action to the below:

[HttpPost]
public virtual JsonResult RenderImage(string code)
{
    var filePath = "~/Images/PngExampleImag.png";
    var ImageBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath));
    var data = Convert.ToBase64String(ImageBytes);
    return Json(data);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried to return png byte array using this code from the RenderImage Action "return Json(System.IO.File.ReadAllBytes(errorFilePath))" and still it is not able to display the image in the view
@user1570697 I edited the answer and added a small modification to the action to make it return the png image file, I tested it and its working with my app. hope this helps.
I tried the controller code with a sample image var filePath = "~/Images/Error.png"; var ImageBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath)); var base64data = Convert.ToBase64String(ImageBytes); return Json(base64data); still the image is not getting updated , eventhough the data is coming to the ajax call back, may be the this is not working properly $('#ChartDiv').html('<img class="iamge" height="400" width="450" src="data:image/png;base64,' + data + '" />'); I use IE 9
I get the whole DIV data through a ajax call , is that because of that it is not setting the image for $('#ChartDiv').html('<img class="iamge" height="400" width="450" src="data:image/png;base64,' + 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.