1

On click of a button, I am making an AJAX request to ASP.NET MVC controller and it will generate an image (QR Code) then provide a byte array as a result like this.

Controller

[HttpPost]
public JsonResult GenerateQRCode()
{
    byte[] QRImage = GenerateImageHere(); //This will generate image and get its byte array

    return Json(QRImage, JsonRequestBehavior.AllowGet);
}

AJAX Request

$.ajax({
    url: '@Url.Action("GenerateQRCode", "Home")',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    dataType: 'JSON',
    cache: false,
    contentType: false,
    processData: false,
    success: function (response) {
        //I need to set generated image in a div
    }
});

I am unable find a way to display the image in the page. How can I make the image visible?

Please note this is not a duplicate question since I did not find any solution

2
  • Just FYI you cannot send a byte array in JSON like that. You would be better off base64 encoding the byte array as a string, then sending that in the JSON. Then you can use the duplicate link in the previous comment to display the image on the front-end. Commented Sep 26, 2019 at 9:26
  • Please update your html code for image. Is the image a dynamic one? Commented Sep 26, 2019 at 10:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.