Last active
July 13, 2018 22:36
-
-
Save nolanlawson/b8a59d3832f25f2420e4 to your computer and use it in GitHub Desktop.
Check XHR responseType = 'blob'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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