Last active
September 21, 2017 15:38
-
-
Save khaosdoctor/cd58b2520bd18ad82336082765180e65 to your computer and use it in GitHub Desktop.
Fetch API example
This file contains hidden or 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
if (self.fetch) { // Verificamos se existe suporte do navegador ao Fetch | |
var myImage = document.querySelector('img'); | |
fetch('arquivo.jpg') // Busca um arquivo de uma URL | |
.then(function(response) { // Trata a resposta em uma promise | |
return response.blob(); // Busca o blob desta resposta | |
}) | |
.then(function(myBlob) { // Passa o blob para outra função | |
var objectURL = URL.createObjectURL(myBlob); // Cria uma URL | |
myImage.src = objectURL; // Adiciona ao SRC da imagem | |
}); | |
} else { // Se não houver podemos fazer o XMLHttpRequest } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment