0

Im having System.Byte[] in SQL 08. i have to convert System.Byte[] into image and to display that image in <img> tag in html. im using jquery to get image from mvc and to display it in html.

Im not using View(V) instead of that im using HTML.

MVC 3 Controller

public ActionResult Get_Leave_IO_Pic(DetailsModel model)  
      {
        TCSServiceReference.MBLServiceSoapClient TCSClient = new TCSServiceReference.MBLServiceSoapClient();
        DataSet ds = new DataSet();
        DataSet resultds = TCSClient.Get_Employee_Details_Srno(ds, model.EMPSRNO);
        Image strJSON = null;
        if (resultds.Tables[0] != null && resultds.Tables[0].Rows != null)
        {
            byte[] byteArrayIn = (byte[])resultds.Tables[0].Rows[0]["EMPPHOT"];
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            strJSON = returnImage;
        }
        return Json(new { result = strJSON });
    }

HTML

 <img src="../images/bis_user.png" width="113" height="104" border="0" alt="" id="ImageStaff" />

jQuery

function GetLeaveIOPic(empsrno) {
   $.post("http://Details/Get_Leave_IO_Pic",
    {
        EMPSRNO: empsrno
    },
    function (data) {
        $.each(data, function (key, value) {
            $('#ImageStaff').val(); //How to get image here?
        });
    }, 'json' );
}

2 Answers 2

1

im using jquery to get image from mvc and to display it in html.

You don't really need javascript to achieve that. You could have your controller action directly return the image contents in the response and set the proper content type:

public ActionResult Get_Leave_IO_Pic(string id)  
{
    var TCSClient = new TCSServiceReference.MBLServiceSoapClient();
    var ds = new DataSet();
    var resultds = TCSClient.Get_Employee_Details_Srno(ds, id);
    if (resultds.Tables[0] != null && resultds.Tables[0].Rows != null)
    {
        byte[] byteArrayIn = (byte[])resultds.Tables[0].Rows[0]["EMPPHOT"];

        // TODO: adjust the image MIME type based on what you have stored
        // in your database
        return File(byteArrayIn, "image/jpeg");
    }

    return HttpNotFound();
}

and in the view simply point the <img> tag to this controller action by passing the id that allows to retrieve the image from the database:

<img src="@Url.Action("Get_Leave_IO_Pic", new { id = "123" })" width="113" height="104" border="0" alt="" id="ImageStaff" />
Sign up to request clarification or add additional context in comments.

1 Comment

Im not using view, instead of that im using HTML. And i have to return the value in strJSON only. Thank you.
1

Finally i got solution for this.

` ======================================= MVC 3 Controller

public ActionResult Get_Leave_IO_Pic(DetailsModel model)
    {
       TCSServiceReference.MBLServiceSoapClient TCSClient = new TCSServiceReference.MBLServiceSoapClient();
        DataSet ds = new DataSet();
        DataSet resultds = TCSClient.Get_Employee_Details_Srno(ds, model.EMPSRNO);
        string strJSON = "Failed";
        if (resultds.Tables[0] != null && resultds.Tables[0].Rows.Count > 0)
        {
            byte[] byteArrayIn = (byte[])resultds.Tables[0].Rows[0]["EMPPHOT"];
            string EmpName = resultds.Tables[0].Rows[0]["EMPNAME"].ToString();
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image gotimage = Image.FromStream(ms);
            string filepath = (System.Configuration.ConfigurationManager.AppSettings.Get("ImageUpload")).ToString(); 
            //filepath = E:/Project/project1/Project1 MVC/Images/
            gotimage.Save(filepath + EmpName + " - " + model.EMPSRNO + ".png");
            strJSON = System.Configuration.ConfigurationManager.AppSettings.Get("ImageURL").ToString() + EmpName + " - " + model.EMPSRNO + ".png";
            //ImageURL = http://localhost:8085/Project1MVC/Images/
        }
        return Json(new { result = strJSON });
    } 

` ======================================= HTML

 <img src="../images/bis_user.png" width="113" height="104" border="0" alt="" id="ImageStaff" />

` ======================================= jQuery

function GetLeaveIOPic(empsrno) {
        $.post("http://localhost:8085/Project1MVC/Details/Get_Leave_IO_Pic",
    {
        EMPSRNO: empsrno
    },
    function (data) {
        $.each(data, function (key, value) {
            if (!(data.result == 'Failed')) {
                // data.result = http://localhost:8085/Project1MVC/Images/name - 23.png
                $('#ImageStaff').attr('src', data.result.toString());
            }
        });
    },
  'json'
 );
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.