Created
December 10, 2019 20:21
-
-
Save trevorblades/78c37f4a35bea49c428f0b13f320aac1 to your computer and use it in GitHub Desktop.
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
async function uploadFile(file, url, handleProgress) { | |
return new Promise((resolve, reject) => { | |
const req = new XMLHttpRequest(); | |
req.upload.addEventListener('progress', handleProgress); | |
req.addEventListener('load', () => resolve(true)); | |
req.addEventListener('error', () => { | |
const error = new Error('Upload failed'); | |
reject(error); | |
}); | |
req.open('PUT', url); | |
req.send(file); | |
}); | |
} | |
const uploaded = await uploadFile( | |
myFile, | |
'https://api.example.com/files', | |
(event) => { | |
console.log(event); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment