Last active
October 24, 2019 14:02
-
-
Save karatechops/6419bbb34ddcdb657f9447726644b5f0 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
export function fileUpload(file) { | |
let formData = new FormData(); | |
for(name in file) { | |
formData.append(name, file[name]); | |
} | |
return fetch('https://my-website.com/api/file/upload', { | |
method: 'POST', | |
// if your app is storing auth tokens in a cookie include credentials | |
// credentials: 'include', | |
body: formData | |
}) | |
.then(response => | |
response.json().then(json => ({ | |
status: response.status, | |
statusText: response.statusText, | |
json | |
}) | |
)) | |
.then( | |
({ status, statusText, json }) => { | |
if (status >= 400) { | |
// API returned a crappy response | |
console.log('error:', status, statusText, json); | |
} else { | |
// Upload done! | |
return json; | |
} | |
}, | |
) | |
.catch(err => console.log(err)); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment