I've been dealing with this problem for a while now apparently you can upload a image using FormData
and appending the file but I am still unable to get this to work. I have already tried with the following threads:
- React Native - Axios - Trying to upload image
- form post with file attach throws network error / React Native + react native Image picker
- How do I set multipart in axios with react?
My first problem revolves around FormData().append()
, in most solutions a blob/file is configured using the uri
property but I don't have access to this property (Is this related to Typescript?). So something like this:
formData.append("profile_photo", {
uri: values.imageURI
});
Here's the error TS is returning:
Argument of type '{ uri: string; }' is not assignable to parameter of type 'string | Blob'.
Object literal may only specify known properties, and 'uri' does not exist in type 'Blob'.ts(2345)
Another key points is setting up my axios client with the Content-Type
header, I've done this during my POST request like so:
// Perform a profile post request
const profileResponse = await loginClient.post(
"/user_profile",
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
}
);
But by logging my request with Axios' interceptors, here how the request headers look like:
headers:
Accept: "application/json"
Authorization: "Bearer 36|8SVw1DntRKni0sUn4NDX9moLluCHyYcZSadfgI5B"
__proto__: Object
Just a few observations:
- The image uri is changed using expo-image-picker.
- I'm using Formik to build my form.
- I'm using Typescript.
formData.append("profile_photo", values.imageURI);
? documentation - oops, nevermind, react-native may be different - sorry – Jaromanda Xvalues
contains my submit form values,values.imageURI
contains the image local uri. – Jeremy