繁体   English   中英

使用Handler.ashx获取图像

[英]Getting an image using 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)返回“(byte [])。cmd.ExecuteScalar();”,我真的不确定我应该对传回的信息做什么。 我假设它是图像本身? 任何帮助表示赞赏。 谢谢!

为什么不试试

function GetImage(id) {

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM