Dear All,
I'm trying to show an image present in the remote server on asp.net core razor view.
I'm fetching the image then converting it to byte and if I pass the bytes through viewbag it works fine.
But, my requirement is to pass the image through jquery ajax call.
Please see the code I tried below.
public IActionResult GetImage(string ItemCode)
{
string filePath = @"\\144.44.12.229\PD New Models\" + ItemCode +".jpg";
byte[] byteData = System.IO.File.ReadAllBytes(filePath);
string imreBase64Data = Convert.ToBase64String(byteData);
string imgDataURL = string.Format("data:image/png;base64,{0}", imreBase64Data);
ViewBag.ImageData = imgDataURL;
return Json(imgDataURL);
}
<img id="imgDesign" src="" alt="design" />
var itemCode = $("#txtItemCode").val();
var url = "@Url.Action("GetImage", "Inventory")";
$.ajax({
url: url,
type: "GET",
data: { ItemCode: itemCode },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#imgDesign").attr("src", response.imgDataURL);
}
});