3

I have the following webapi method

    public HttpResponseMessage GetImage()
    {
        string imgUri = HttpContext.Current.Server.MapPath("~/Images/htmllogo.png");
        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(imgUri, FileMode.Open));

        response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

        return response;
    }

I also have the following html

 <div><button id="loadImage">Load</button></div>
<div id="divImageHolder"></div>

I am using jquery's Ajax method to retrieve my image like this:

$("#loadImage").click(function () {
        var options = {};
        options.url = "api/mywebapi/GetImage";
        options.type = "GET";
        //options.dataType = "image/png";
        options.contentType = "image/png";
        options.success = function (results) {          
            var imag = "<img " + "src='" + results + "'/>";

            $("#divImageHolder").html(imag);             

        };
        options.error = function (jqXHR, textStatus, errorThrown)
        {
            console.log(errorThrown);
        };
        $.ajax(options);
    });    

What I see after clicking the button is 'binary garbage' in divImageHolder div. No image is being displayed

I have tried adding "data:image/png;base64" to src property before the result but that didn't work. I think the reason is that base64 is text encoding but what I am receiving is binary data.

If I use Json and convert the image into base64 encoding (in the web api method) then it works fine however I couldn't manage to get it to work with binary i.e. my web api method is returning StreamContent.

options.dataType is commented out on purpose because it doesn't accept anything but json. If I uncommented it, I will get an error. The error is "No conversion from text to image/png" but if I leave it commented out I manage to get to success method where I can see the binary result

Thank you

Sul

Sul Aga
  • 6,142
  • 5
  • 25
  • 37
  • You're setting the resulting data to the image... If you're just calling a get, then set the source of the image to the url... – Phill May 25 '14 at 03:37
  • can you elaborate on this please? can you write it in code. Please note that the web api is returning HttpResponseMessage – Sul Aga May 25 '14 at 03:42
  • your suggestion worked, I just tried it however I still would like to see if there is a way of using the retrieved binary data to display the image using jquery Ajax method – Sul Aga May 25 '14 at 03:47
  • Um, sorta, the problem is browsers don't really support it, as far as I know, a binary data type. You get a byte array you need to shift to create an object. Once you have that object tho its a case of just calling `URL.createObjectURL(data)` and assigning it to a image src. Sorta like this (this is all done client side but with a file control, so slightly different, just showing the URL thing) http://www.philliphaydon.com/2014/04/loading-an-image-or-video-from-a-file-input-control/ – Phill May 25 '14 at 04:46
  • Data Urls: http://en.wikipedia.org/wiki/Data_URI_scheme Lots of reasons why you shouldn't do that except for all but the simplest images. @Phill's suggestion is the way to go – Prescott May 25 '14 at 07:41
  • I have tried @Phill suggestion and passed the result from my web api call to window.URL.createObjectURL. but I got this error: ncaught TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided – Sul Aga May 25 '14 at 12:10
  • What browser are you using? It's only supported in IE10+ and all modern browsers. – Phill May 25 '14 at 12:18
  • I am using Chrome Version 35.0 which is the latest version at the moment. I have tried this example in the browser and it works fine: https://developer.mozilla.org/samples/domref/file-click-demo.html I am assuming that the binary result I am passing in the createObjectURL function is some how not compatible. – Sul Aga May 25 '14 at 12:27

1 Answers1

0

This post here by @gaetanoM should solve this problem, basically by doing the base64 converson in the javascript

Also the ' mimeType: "text/plain; charset=x-user-defined"' may be necessary.

Community
  • 1
  • 1
J. Allen
  • 620
  • 5
  • 12