Javascript approach with Base64.js library
Since u indicated that u wants to convert it using JavaScript, you can do this using the Base64 JavaScript library. It has the following methods:
toByteArray - takes a base64 string and returns an array of bytes
and
fromByteArray - takes a byte array and returns a base64 string
Here is an example of how you could do this. First, back from ASP.NET MVC as the answer:
return Json(new { Img = imageBytes }, JsonRequestBehavior.AllowGet);
And then on the client, you can use the jQuery getJson () function to get the JSON response from MVC ImageController:
$.getJSON("/Image",function(result){ $.each(result, function(i, field){ var byteArray = result.Img; var base64 = base64js.fromByteArray(byteArray); $("#mainimg").attr('src', 'data:image/jpeg;base64,' + base64); }); });
You can download the base64.js library from here:
https://github.com/beatgammit/base64-js
C # Conversion Approach
If you don't like the base64 javascript approach, you can convert the byte array on the server using C # using:
String base64string = Convert.ToBase64String(imageBytes); return Json(new { Img = base64string }, JsonRequestBehavior.AllowGet);
This will return a Base64 string as a result of the JSON Result. Then you can easily set the img source from jQuery, as shown in the Javascript Approach example.