1

Possible Duplicate:
Getting BLOB data from XHR request

I try to get an image from my server dynamically, but the XMLHTTPRequest returns null in the response. The Google Chrome network tool tells me that he loaded the day1.jpg, but the blob variable is null...

var xhr = new XMLHttpRequest(), blob;

xhr.open("GET", "day1.jpg", true);

// Set the responseType to blob
xhr.responseType = "blob";

xhr.addEventListener("load", function () {
    if (xhr.status === 200) {
        console.log("Image retrieved");
        blob = xhr.response;
        console.log("blob: " + blob);
    }
}, false);

// Send XHR
xhr.send();

The output is:

Image retrieved
blob: null
4
  • If its returning status code 200 and actually running there shouldn't be an issue. Try putting console.log("blob: " + xhr.response) for grins. Commented Mar 5, 2012 at 17:54
  • blob = xhr.response; console.log("blob: " + blob); is exactly the same as console.log("blob: " + xhr.response) Commented Mar 5, 2012 at 19:19
  • should be exactly the same, just like the code above should be working. ;p Commented Mar 5, 2012 at 19:21
  • yeah as zuuum wrote it is a bug in chrome ;) Commented Mar 5, 2012 at 19:36

4 Answers 4

4

The reason is a bug on the Chrome side (also available on v18). Reported here

Sign up to request clarification or add additional context in comments.

1 Comment

html5rocks.com/en/tutorials/file/xhr2 <-- Here you can see the workaround.
1

why use ajax to load the image (as far as I know you can't http://www.mail-archive.com/[email protected]/msg00377.html)? You can just dynamically generate an image element and set the src attribute to the image on your server.

var i = new Image();
i.onload = function()
{
   //do the stuff you need to do once it loads here
}
i.src = "day1.jpg"; 

1 Comment

It is possible with HTML5. And I want to load a pdf file in the end.
0

Use the console to inspect the xhr object and see if it's being populated at all

3 Comments

If the handler is called and Google Chrome tells me it is loaded, why shouldn't it be populated?
Try it and see what is populated in the object then.
The response is null, the status 200, the readyState 4 and everything is null. But I already told that in my question...
-1

I doubt that you can load images from the server dynamically, instead what you can do is update the image source dynamically and tell from whci location shoul the image be fetched/loaded

2 Comments

Why wouldn't he be able to? All it does it call a simple GET request (exactly like a browser would do if you typed in the URL) and returns the response (the image).
and how is he supposed to use the returned data???

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.