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

Andrei
  • 55,890
  • 9
  • 87
  • 108
A.M.Roomi
  • 113
  • 2
  • 10
  • 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](https://stackoverflow.com/questions/12178495/an-efficient-way-to-base64-encode-a-byte-array), 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. – Rory McCrossan Sep 26 '19 at 09:26
  • Please update your html code for image. Is the image a dynamic one? – Ajoe Sep 26 '19 at 10:30

0 Answers0