I'm making an app for my clients to create and edit content on their wordpress site. I'm stuck on a problem when trying to upload images.
I'm using vue 3 and ionic as my framework and currently testing on an android phone. Im sending a POST request with this fetch:
const uploadPhotos = async () => {
cameraStatus.value = 'uploading started'
await store.create();
const photoList = await store.get('photos');
const photosInStorage = photoList ? JSON.parse(photoList) : [];
const files = await photosInStorage.map((photo: any) => {
return Filesystem.readFile({
path: photo.filepath,
})
})
const filesUploaded = await Promise.all(files)
cameraStatus.value = 'gotten images'
console.log(filesUploaded)
for(let i = 0; i < filesUploaded.length; i ) {
const file = filesUploaded[i];
cameraStatus.value = 'uploading' file
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "file/jpeg",
"Content-Disposition": "attachment; filename=" new Date().getTime() '.jpeg',
'Authorization': 'basic ' Buffer.from(wpUsername ":" wpApplicationPassword).toString('base64')
},
body: JSON.stringify({
file: file.data
})
}
await fetch(wpSite '/wp-json/wp/v2/media',
requestOptions
)
.then(response => response.json())
.then(data => {
cameraStatus.value = data;
}).catch(error => {
cameraStatus.value = error;
})
}
}
. It works when uploading it as .txt put not as .jpg, .jpeg or .png. It returns the error: "Sorry you are not allowed to upload this file type" when i try to upload any of the image types.
I have tried disabling all plugins and themes but with no luck.
I tried contacting the hosting provider and they said it is on my end.
Any ideas what could cause this problem?
CodePudding user response:
Found the answer here:
Creating a BLOB from a Base64 string in JavaScript
I used the b64toBlob function on the file.data instead of stringify and it worked.