使用Javascript下载图像并将其转换为Blob

时间:2015-07-01 19:18:05

标签: javascript html5

使用HTML5和FileReader / Blob我可以从/转换为Blob / dataURI,但我想知道我是否可以从远程URL下载图像并将其转换为Blob(或dataURI)。

如果可以,我该怎么做?

1 个答案:

答案 0 :(得分:0)

我自己设法做到了:

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