Skip to content

Instantly share code, notes, and snippets.

@olivx
Created November 1, 2018 02:23
Show Gist options
  • Save olivx/ade7f3786c2484a6bf28e2547634b3e3 to your computer and use it in GitHub Desktop.
Save olivx/ade7f3786c2484a6bf28e2547634b3e3 to your computer and use it in GitHub Desktop.
estrutura exemplo de vanilla javascript fazendo upload vai ajax
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