alexandernst
alexandernst

Reputation: 15099

Download image with Javascript and convert it to a Blob

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?

Upvotes: 2

Views: 5828

Answers (1)

alexandernst
alexandernst

Reputation: 15099

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();

Upvotes: 4

Related Questions