2012-03-29 51 views
1

我目前正在进行一个学校项目,我不确定返回的内容以及如何使数据可用。下面是代码:使用Handler.ashx获取图像

Default.aspx的

function GetImage(id) { 
     //step k. - code here 
     xmlHttpObj = CreateXmlHttpRequestObject(); 
     if (xmlHttpObj) { 
      xmlHttpObj.open("GET", "Handler.ashx?id=" + id, true); 
      xmlHttpObj.send(null); 
      var image = document.getElementById("ProductImage"); 
      //the response contains an array of 5419 index 
     } 
    } 

Handler.ashx

public void ProcessRequest (HttpContext context) 
    { 
     int id; 
     if (context.Request.QueryString["id"] != null) 
     { 
      id = Convert.ToInt32(context.Request.QueryString["id"]); 
      context.Response.ContentType = "image/jpeg"; 
      byte[] bufferImg = GetImage(id); 
      context.Response.OutputStream.Write(bufferImg, 0, bufferImg.Length); 
     } 
    } 

的getImage(int id)返回 “(字节[])cmd.ExecuteScalar();”,即时通讯不确定我应该如何处理正在传回的信息。我假设它是图像本身?任何帮助表示赞赏。谢谢!

回答

3

为什么不尝试

function GetImage(id) { 

      document.getElementById("ProductImage").src="Handler.ashx?id=" + id; 

    } 
+0

谢谢!像魅力一样工作。我之所以需要使用xmlHttpObject,是因为我从来没有这样做过。它很好地刷新闪光灯,时刻记住总有另一种方式。 – 2012-03-29 17:49:20