0

我在 SQL 08 中有 System.Byte[]。我必须将 System.Byte[] 转换为图像并<img>在 html 的标记中显示该图像。我使用 jquery 从 mvc 获取图像并将其显示在 html 中。

我不使用 View(V) 而不是使用 HTML。

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

2 回答 2

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

并在视图中<img>通过传递允许从数据库中检索图像的 id 将标签指向此控制器操作:

<img src="@Url.Action("Get_Leave_IO_Pic", new { id = "123" })" width="113" height="104" border="0" alt="" id="ImageStaff" />
于 2012-04-23T09:29:05.800 回答
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'
 );
    }
于 2012-06-06T12:03:21.910 回答