Last active
August 29, 2015 14:14
-
-
Save victorvhpg/6b8515c623b2045fc074 to your computer and use it in GitHub Desktop.
faz download de arquivo com responseType ArrayBuffer //resolve a Promise com o ArrayBuffer do arquivo
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
//retorna uma Promise que faz download de urlArquivo com responseType ArrayBuffer | |
//resolve a Promise com o ArrayBuffer do arquivo | |
var downloadArquivo = function(urlArquivo,fnProgresso) { | |
return new Promise(function(resolve, reject) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", urlArquivo, true); | |
//setar o responseType para arraybuffer | |
xhr.responseType = "arraybuffer"; | |
//quando a requisicao completar | |
xhr.addEventListener("load", function() { | |
//se nao for 200 entao deu algum erro | |
if (this.status !== 200) { | |
reject("Erro ajax: " + this.statusText); | |
return; | |
} | |
//devido ao responseType ser "arraybuffer" | |
//o this.response ja esta "parseado" e eh um | |
//ArrayBuffer | |
console.log(this.response instanceof ArrayBuffer); // true | |
var arrayBuffer = this.response; | |
if (arrayBuffer) { | |
resolve(arrayBuffer); | |
} else { | |
reject("arrayBuffer vazio"); | |
} | |
}, false); | |
//progresso do download | |
xhr.addEventListener("progress", function(e) { | |
if (fnProgresso) { | |
if (e.lengthComputable && e.total > 0) { | |
console.log(e.loaded, e.total, (100 * e.loaded) / e.total); | |
fnProgresso(e.loaded, e.total, (100 * e.loaded) / e.total); | |
} else { | |
console.log("nao foi possivel obter tamanho total"); | |
} | |
} | |
}, false); | |
//quando ocorrer algum erro na requisicao | |
xhr.addEventListener("error", function() { | |
console.log("error", arguments); | |
console.log(this); | |
reject("Erro no ajax"); | |
}, false); | |
xhr.send(null); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment