'Upload image to a nodeJs server using react-native
I want to upload an image from react-native (cli without expo) to a node server, i tried using axios with formData filed is not recognised by multer, then i tried with fetch that worked image was stored in server but i get promise rejection on front:
const postDocument = (doc) => {
//i used react-native-document-picker to pick document(doc) i logged it and it works
const url =`${api}/sendImage`;
const formData = new FormData();
const id = uuid.v4()
formData.append('id',id)
formData.append('image', doc);
const options = {
method: 'POST',
body: formData,
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
};
fetch(url, options).then((res)=>{
//getting promise rejection even without code but image is stored in backend
}).catch(error=>{})
}
getting this error : [TypeError: Network request failed]
Solution 1:[1]
using rn-fetch-blob (react-native-cli not expo-cli) instead of fetch or axios solved this for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Giverseal |