2

With HTML5 and FileReader/Blob I can convert from/to Blobs/dataURIs, but I'm wondering if I can download an image from a remote URL and convert it to a Blob (or a dataURI).

If this is possible, how would I do it?

3
  • xhr.responseType="blob"; Commented Jul 1, 2015 at 19:38
  • @dandavis Not very helpful, but it gave me a clue where to look at. Commented Jul 1, 2015 at 21:18
  • if you would have posted code, i could have fixed it for you. i could have been more specific i guess, but i'm glad you got it working. Commented Jul 1, 2015 at 22:04

1 Answer 1

4

I managed to do it myself:

var xhr = new XMLHttpRequest();
xhr.open('GET', attr.ngfDefaultSrc, true);
xhr.responseType = 'blob';

xhr.onload = function(e) {
   if (this.status !== 200) return;
   var blob = new Blob([this.response], {type: this.response.type});
   //rest of the code that uses the blob goes here
};

xhr.send();
Sign up to request clarification or add additional context in comments.

1 Comment

this.response is already a blob, and the mime should be set already by the Content-Type header, so you probably don't need the extra step of concating the blob into a new blob. var blob=this.response;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.