使用Handler.ashx获取图像

Getting an image using Handler.ashx

本文关键字:图像 获取 ashx Handler 使用      更新时间:2023-09-26

我目前正在做一个学校项目,我不确定返回了什么以及如何使数据可用。这是代码:

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
        }
    }

句柄.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)返回"(byte[]).cmd.ExecuteScaler();",我真的不确定该如何处理传递回来的信息。我想是图像本身吧?任何帮助都将不胜感激。谢谢

为什么不尝试

function GetImage(id) {
            document.getElementById("ProductImage").src="Handler.ashx?id=" + id;
    }