[英]Error: Network Error Axios - Image React Native
Im trying to post an Image to server but axios throw an error saying我试图将图像发布到服务器但 axios 抛出错误说
[Error: Network Error]
[错误:网络错误]
I created a bodyFormData and appended the following我创建了一个 bodyFormData 并附加了以下内容
bodyFormData.append('image', {
name: imgName,
type: 'image/jpeg',
uri: this.state.image,
});
where my imageName
is just a string and the this.state.image
is the file path file:///storage/emulated/0/Pictures/0cccb0f1-375b-44b8-b15b-4625536a8d63.jpg
其中我的
imageName
只是一个字符串,而this.state.image
是文件路径file:///storage/emulated/0/Pictures/0cccb0f1-375b-44b8-b15b-4625536a8d63.jpg
Axios Call Axios 调用
const addProduct = async product => {
try {
var prod = await axios({
method: 'post',
url: baseURL + 'addProduct',
data: product,
headers: {
'content-type': `multipart/form-data; boundary=${product._boundary}`,
Accept: 'application/json',
},
},
});
return prod;
} catch (err) {
throw err;
}
};
after alot of trials i understand that in bodyFormData
even i write any kind of json object经过大量试验后,我了解到在
bodyFormData
中,即使我写了任何类型的 json object
const oj = {
name: 'a',
c: 8,
}
bodyFormData.append('image', obj );
it throws me the same error它给我带来了同样的错误
[Error: Network Error]
[错误:网络错误]
Kindly please drop the best possible solution for the problem请放弃该问题的最佳解决方案
Thank you for your precious time.感谢您的宝贵时间。
I recently implemented this (working) code我最近实现了这个(工作)代码
const formData = new FormData();
formData.append('file', {
type: 'image/jpeg',
name: `myname.jpeg`,
uri: isAndroid() ? uri : uri.replace('file://', ''),
});
and with this header in the api call并在 api 调用中使用此 header
'Content-Type': 'multipart/form-data'
It's very similar to your code - maybe the uri part then?它与您的代码非常相似-也许是uri部分?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.