Search code examples
javascriptasp.netashx

Getting an image using Handler.ashx


I am currently working on a school project and I'm not sure what is being returned and how to make the data usable. Here is the code:

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) returns "(byte[]).cmd.ExecuteScalar();", im not really sure what i am supposed to do with the information that is being passed back. I'm assuming it is the image itself? any help is appreciated. Thanks!


Solution

  • why not try

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