download progress for images

adobe proposal:

http://blogs.adobe.com/webplatform/2012/01/13/html5-image-progress-events/

workaround:

function loadImage(imageUrl, onprogress) {

return new Promise(function(resolve, reject) {

var xhr = new XMLHttpRequest();

var notifiedNotComputable = false;

xhr.open('GET', imageUrl, true);

xhr.responseType = 'arraybuffer';

xhr.onprogress = function(ev) {

if (ev.lengthComputable) {

onprogress(parseInt((ev.loaded / ev.total) * 100));

}

else

{

if (!notifiedNotComputable) {

notifiedNotComputable = true;

onprogress(-1);

}

}

};

xhr.onloadend = function() {

if (!xhr.status.toString().match(/^2/)) {

reject(xhr);

} else {

if (!notifiedNotComputable) {

onprogress(100);

}

var options = {};

var headers = xhr.getAllResponseHeaders();

var m = headers.match(/^Content-Type\:\s*(.*?)$/mi);

if (m && m[1]) {

options.type = m[1];

}

var blob = new Blob([this.response], options);

resolve(window.URL.createObjectURL(blob));

}

};

xhr.send();

});

}

你可能感兴趣的:(download progress for images)