make read file and upload separate js functions

This commit is contained in:
Johannes Heuel
2023-03-05 11:54:24 +01:00
parent f5af0c8711
commit fbcea9e77b
2 changed files with 34 additions and 24 deletions

View File

@@ -49,7 +49,7 @@ export function get_files_data_transfer_items(data_transfer_items) {
return getFilesDataTransferItems(data_transfer_items);
}
function read_file(file) {
export function read_file(file) {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onloadend = () => {
@@ -60,23 +60,21 @@ function read_file(file) {
});
}
export function upload_file(file, url) {
export function upload(content, content_type, url) {
return new Promise((resolve, reject) => {
read_file(file).then((content) => {
fetch(url, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: new Blob([content], { type: file.type }),
}).then((resp) => {
if (resp.status >= 200 && resp.status < 300) {
resolve(resp);
} else {
reject(resp);
}
});
}).catch(error => reject(error));
fetch(url, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: new Blob([content], { type: content_type }),
}).then((resp) => {
if (resp.status >= 200 && resp.status < 300) {
resolve(resp);
} else {
reject(resp);
}
});
});
}