1

I have an img control/element on my asp.net page.

At the moment i am call an ashx handler to retrn a byte array using context type as:

context.Response.ContentType = "image/jpg";

The javascript sets the image like this:

img.src = url + '/Mobile/GetFrame.ashx?';

Is there a way to do the same but using a JQuery/Ajax call instead?

So, for instance:

function GetImage() {
    $.ajax({
        url: url + '/Mobile/GetFrame.ashx',
        type: 'GET',
        timeout: freshWaitlimit,
        data: lastTS,
        success: function (response, status, xhr) {
            serverImage1x4.src = response;
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('err');
            finished = true;
        }
    });
}

But I get no image. But i get no error either..

I have my solution but it really is not all my own work. Here it is:

function GetImage() {
    var xmlHTTP = new XMLHttpRequest();
    xmlHTTP.open('GET', url + '/Mobile/LiveXP.ashx?Alias=' + Alias + '&camIndex=' + camIndex + '&Guid=' + createGuid(), true);
    xmlHTTP.responseType = 'arraybuffer';
    xmlHTTP.timeout = 10000;
    xmlHTTP.ontimeout = function () { alert("Timed out!!!"); finished = true; }
    xmlHTTP.onload = function (e) {
        var arr = new Uint8Array(this.response);
        var raw = String.fromCharCode.apply(null, arr);
        var b64 = btoa(raw);
        var dataURL = "data:image/jpeg;base64," + b64;
        serverImage1x4.src = dataURL;
    };
    xmlHTTP.send();
}
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 4
    possible duplicate of [Using raw image data from ajax request for data URI](http://stackoverflow.com/questions/20035615/using-raw-image-data-from-ajax-request-for-data-uri) – Barmar Feb 11 '14 at 15:07
  • What's the benefit of doing it this way? – Barmar Feb 11 '14 at 15:07
  • Hi Barmar. 1st thanks for that link. Looking at it now. The only reason I am considering this is so that I can use the time-out parameter to abort the call if it takes too long. So, for instance, if the server is down, busy etc I can abort the call. That's the theory anyway – Andrew Simpson Feb 11 '14 at 15:09
  • @closers Hi, I would close this myself but the solutions proposed do not seem to work – Andrew Simpson Feb 11 '14 at 15:15
  • that solution is not applicable to the context I wanted to use it as. Unless i am missing something? – Andrew Simpson Feb 11 '14 at 15:17
  • 1
    Did you look at both answers? the second one is likely more cross-browser friendly. (though you'll of course have to do your own research) – Kevin B Feb 11 '14 at 15:18
  • everyone pls bear with me :) The scenario is slightly different to what I have. I am trying now to see if it can be slightly modified. If it can I will post the answer here. – Andrew Simpson Feb 11 '14 at 15:21
  • The difference is that the image is stored as a byte array on the server. – Andrew Simpson Feb 11 '14 at 15:23
  • 1
    that's fine, convert it to base64 and return it. – Kevin B Feb 11 '14 at 15:34
  • If i convert it to base64 1st the data packet to download will inflate – Andrew Simpson Feb 11 '14 at 15:38
  • I do have a solution now based on that link which i will post in a second. – Andrew Simpson Feb 11 '14 at 15:38
  • It is only a very small tweak so not sure if I should still close this question. I will post and let u people tell me what to do. Thanks all for the pointers – Andrew Simpson Feb 11 '14 at 15:39

0 Answers0