unable to upload via the api/upload endpoint.
alucav opened this issue · 1 comments
alucav commented
I am getting the error "files are empty" in response to my api/upload request.
async function uploadFile(file: FileList, field: string){
// initialize the FormData object
let fd = new FormData();
// set the FormData value to the uploaded file.
fd.set(`files.${field}`, file[0], file[0].name);
// we've got what we need. send it to the API.
const res = await fetch(cms + `/api/upload`, {
method: 'POST',
headers: {
// use the locally stored JWT as a bearer token authorization
Authorization: `Bearer ${$page.data.token}`,
// we are sending json
'accept': 'application/json',
'content-type': file[0].type,
},
// update the user data by key and value, which is sent through the value of the form fields.
body: fd
});
let response = await res.json();
if (res.ok) {
console.log(response)
} else {
console.log(response.error)
}
}
// initialize the banner field value
let banner: FileList | null = null;
// reactive. if a file has been added by the user
$: if (banner != null && banner.length) {
// upload the file through the api
uploadFile(banner, 'banner')
}