简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM