Wire up file upload form
This commit is contained in:
29
src/services/uploadFile.js
Normal file
29
src/services/uploadFile.js
Normal 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);
|
||||
};
|
||||
Reference in New Issue
Block a user