Created
November 1, 2018 02:23
-
-
Save olivx/ade7f3786c2484a6bf28e2547634b3e3 to your computer and use it in GitHub Desktop.
estrutura exemplo de vanilla javascript fazendo upload vai ajax
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
function uploadFile (file) { | |
return new Promise( (resolve, reject) => { | |
const xhr = new XMLHttpRequest() | |
let fd = new FormData() | |
fd.append('the-file', file) | |
xhr.open('post', '/') | |
xhr.onerror = reject | |
xhr.onload = event => { | |
resolve() | |
} | |
if (xhr.upload) { | |
xhr.upload.onprogress = progress => { | |
console.log(Math.round((progress.loaded * 100) / progress.total) + '%') | |
} | |
} else { | |
// tratamento em navegadores que não suportam xhr.upload | |
} | |
xhr.send(fd) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment