Skip to content

Instantly share code, notes, and snippets.

@trevorblades
Created December 10, 2019 20:21
Show Gist options
  • Save trevorblades/78c37f4a35bea49c428f0b13f320aac1 to your computer and use it in GitHub Desktop.
Save trevorblades/78c37f4a35bea49c428f0b13f320aac1 to your computer and use it in GitHub Desktop.
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