Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active July 13, 2018 22:36
Show Gist options
  • Save nolanlawson/b8a59d3832f25f2420e4 to your computer and use it in GitHub Desktop.
Save nolanlawson/b8a59d3832f25f2420e4 to your computer and use it in GitHub Desktop.
Check XHR responseType = 'blob'
<html>
<body>
<h1>Check XHR blob support</h1>
<p>If you see two images below, then blobs are supported.</p>
<img id="bunny" src="baby-bunny.jpg"/>
<script src="index.js"></script>
</body>
</html>
(function () {
'use strict'
document.addEventListener('DOMContentLoaded', function() {
var input = document.getElementById('bunny');
var src = input.src;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var blob = xhr.response;
var h1 = document.createElement('h1');
h1.innerHTML = 'Downloaded via XHR as blob:';
document.body.appendChild(h1);
var output = document.createElement('img');
var URL = window.URL || window.webkitURL;
output.src = URL.createObjectURL(blob);
document.body.appendChild(output);
}
};
xhr.open('GET', src, true);
xhr.responseType = 'blob';
xhr.send();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment