improved file uploads
This commit is contained in:
@@ -131,7 +131,8 @@ export async function deleteWarrenEntry(
|
||||
|
||||
export async function uploadToWarren(
|
||||
path: string,
|
||||
files: FileList
|
||||
files: FileList,
|
||||
onProgress: ((loaded: number, total: number) => void) | undefined
|
||||
): Promise<{ success: boolean }> {
|
||||
const body = new FormData();
|
||||
for (const file of files) {
|
||||
@@ -147,16 +148,31 @@ export async function uploadToWarren(
|
||||
rest = '/' + rest;
|
||||
}
|
||||
|
||||
const { status } = await useFetch(
|
||||
getApiUrl(`warrens/${warrenId}/upload${rest}`),
|
||||
{
|
||||
method: 'POST',
|
||||
body,
|
||||
key: 'upload-' + new Date().getTime().toString(),
|
||||
}
|
||||
);
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', getApiUrl(`warrens/${warrenId}/upload${rest}`));
|
||||
xhr.upload.onprogress = (e) => {
|
||||
onProgress?.(e.loaded, e.total);
|
||||
};
|
||||
|
||||
if (status.value !== 'success') {
|
||||
const promise = new Promise<void>((res, rej) => {
|
||||
xhr.onreadystatechange = (_) => {
|
||||
if (xhr.readyState !== 4) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xhr.status === 200) {
|
||||
res();
|
||||
} else {
|
||||
rej();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
xhr.send(body);
|
||||
|
||||
try {
|
||||
await promise;
|
||||
} catch {
|
||||
toast.error('Upload', {
|
||||
id: 'UPLOAD_FILE_TOAST',
|
||||
description: `Failed to upload`,
|
||||
|
||||
Reference in New Issue
Block a user