I want to show image through ajax by web method.
<asp:Image Height="200px" Width="200px" runat="server" ID="img_Product" CssClass="col-md-2 control-label labelfont"></asp:Image>
Here is my ajax code,
$(document).ready(function () {
var productValue = <%=Session["StockId"] %>;
var Data = JSON.stringify({ productId: productValue });
$.ajax({
type: "POST",
url: "Image.aspx/GetImageByStockId",
data: Data,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
$("#img_Product").attr("src", data.d.Image);
alert($("#img_Product").attr("src", data.d.Image));
},
error: function (xhr, status, error) {
alert(error + status + xhr);
}
});
});
Here is my webmethod code,
[WebMethod]
public static ImageModel GetImageByStockId(int productId)
{
Services service = new Services();
ImageModel imageModel = new ImageModel();
string qrySelImage = "Select * from tbl_StockRunning where StockId = " + productId + "";
DataTable dtSelImage = new DataTable();
dtSelImage = service.GetDataTable(qrySelImage);
if (dtSelImage.Rows[0]["DesignImage"].ToString() != "")
{
byte[] pic = Encoding.ASCII.GetBytes(dtSelImage.Rows[0]["DesignImage"].ToString());
var data = (byte[])(dtSelImage.Rows[0]["DesignImage"]);
imageModel.Image = "data:image/jpg;base64," + Convert.ToBase64String(data);
}
return imageModel;
}
Now, when I run the code, it shows error i.e.
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. [track by console]