Wire up file upload form

This commit is contained in:
jwu-fisker
2021-01-07 14:10:58 -08:00
parent 8fc6b3b6d8
commit 0ae42bf51d
7 changed files with 145 additions and 16 deletions

View File

@@ -0,0 +1,29 @@
import axios from 'axios';
const UPLOAD_ENDPOINT = 'http://localhost:8080/api/upload';
export const getCancelToken = () => {
const token = axios.CancelToken;
return token.source();
}
export const uploadFile = (file, onProgress, cancelToken) => {
const form = new FormData();
let options = {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
cancelToken,
};
if (onProgress) {
options = {
...options,
onUploadProgress: (event) => {
onProgress(Math.floor((event.loaded / event.total) * 100));
}
}
}
form.append('file', file);
return axios.post(UPLOAD_ENDPOINT, form, options);
};