2012-04-23 46 views
0

在SQL 08中,我有System.Byte []。我必須將System.Byte []轉換爲圖像並在<img>中顯示該圖像在html中標記。即時通訊使用jQuery從mvc中獲取圖像並在html中顯示它。從SQL 08獲取System.Byte []並使用jQuery將其顯示爲HTML圖像

我沒有使用視圖(V)而不是使用HTML的IM。

MVC 3控制器

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'); 
} 

回答

1

最後我得到了解決方案。

`======================================= MVC 3控制器

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' 
); 
    } 
+0

+1它清除了我的周問題。它的工作很好。 – 2013-07-02 06:15:37

1

即時通訊使用jquery從mvc中獲取圖像並將其顯示在html中。

你不需要JavaScript來實現這一點。你可以有你的控制器動作直接在回覆中返回的圖像內容,並設置適當的內容類型:

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(); 
} 

,並在視圖中通過簡單地傳遞,使檢索id指向的<img>標籤此控制器操作從數據庫中的圖像:

<img src="@Url.Action("Get_Leave_IO_Pic", new { id = "123" })" width="113" height="104" border="0" alt="" id="ImageStaff" /> 
+0

我不使用視圖,而是使用HTML即時通訊。我必須只返回strJSON中的值。謝謝。 – 2012-04-24 05:17:37

相關問題