Skip to content

Instantly share code, notes, and snippets.

@karatechops
Last active October 24, 2019 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karatechops/6419bbb34ddcdb657f9447726644b5f0 to your computer and use it in GitHub Desktop.
Save karatechops/6419bbb34ddcdb657f9447726644b5f0 to your computer and use it in GitHub Desktop.
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