My goal is to send a photo from the local device to server.
I used import { launchImageLibrary } from "react-native-image-picker";
to select an asset called photo
and used
const resp = await fetch(photo.uri);
const blob = resp.blob();
formData.append('regPhoto', blob);
registerUser(formData);
to append this as a blob to the form data. I checked that the await fetch call is returnin 200 with meaningful data. Then this formData sent with XMLHttpRequest API:
function registerUser(formData: FormData) {
cl("registerUser");
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open("POST", url + "registerUser");
xhr.onreadystatechange = () => {
const resObj = onReadyStateChange(xhr);
if (resObj) {
resolve(resObj);
}
};
xhr.send(formData);
});
}
Two things happen, the server receives the request without the image and processes it without the image. Which is success to some degree. At the same time, there is a JSON response at the client (android emulator) that reads: Unrecognized formdata part. I am not sure where this is coming from actually.
Does anybody have an idea whats going on here? Just wanted to upload an image to my server via formData.
I tried other methods of appending the photo to the formData but I get the same error.