add function to upload files to seqta's storage

This commit is contained in:
SethBurkart123
2023-11-27 17:21:45 +11:00
parent eb4d834c98
commit 9c1ee08c2e
2 changed files with 46 additions and 3 deletions
+43
View File
@@ -0,0 +1,43 @@
/**
* Uploads an image file to a specified endpoint using a POST request.
*
* @param {File} file - The image file to be uploaded.
* @returns {Promise} A promise that resolves to the response from the server.
* @throws {Error} If no file is provided or if there is an error during upload.
*/
export async function UploadImage(file) {
// Ensuring that file is provided
if (!file) {
throw new Error("No file provided");
}
// Extracting the filename
const fileName = file.name;
// Assuming 'document.cookie' contains all the cookies in the format you provided
const cookies = document.cookie;
// Setting up the request options
const requestOptions = {
method: 'POST',
headers: {
'Cookie': cookies,
'X-File-Name': fileName
},
body: file // Binary file data
};
// Making the fetch request and returning the promise
return fetch('/seqta/student/file/upload/xhr2', requestOptions)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const json = response.json();
return `/seqta/student/load/file?type=message&file=${json.uuid}`;
})
.catch(error => {
console.error('Error during file upload:', error);
throw error;
});
}