admin管理员组文章数量:1426617
I'm trying to upload an image from my app to API, but it didn't responded by the server, but another data which is text is sent successfully, can anyone help me?
Response from the image picker
{"fileName": "rn_image_picker_lib_temp_62670a6c-c6a8-47a6-ae8f-2696455d141b.jpg", "fileSize": 82214, "height": 800, "type": "image/jpeg", "uri": "content://.talikasih.imagepickerprovider/cacheDir/rn_image_picker_lib_temp_62670a6c-c6a8-47a6-ae8f-2696455d141b.jpg", "width": 379}
and this is the data
const datas = new FormData();
datas.append('images', payload.image)
Here is the axios
axios({
method: 'POST',
url: API_NO_PARAM_CONFIG.createCampaign,
headers: {
Authorization: `Bearer ${e}`,
},
data: {
datas,
},
})
I'm trying to upload an image from my app to API, but it didn't responded by the server, but another data which is text is sent successfully, can anyone help me?
Response from the image picker
{"fileName": "rn_image_picker_lib_temp_62670a6c-c6a8-47a6-ae8f-2696455d141b.jpg", "fileSize": 82214, "height": 800, "type": "image/jpeg", "uri": "content://.talikasih.imagepickerprovider/cacheDir/rn_image_picker_lib_temp_62670a6c-c6a8-47a6-ae8f-2696455d141b.jpg", "width": 379}
and this is the data
const datas = new FormData();
datas.append('images', payload.image)
Here is the axios
axios({
method: 'POST',
url: API_NO_PARAM_CONFIG.createCampaign,
headers: {
Authorization: `Bearer ${e}`,
},
data: {
datas,
},
})
Share
Improve this question
edited Jan 22, 2021 at 9:32
Ali Esmailpor
1,2013 gold badges12 silver badges23 bronze badges
asked Jan 22, 2021 at 3:38
amerwamerw
4275 silver badges14 bronze badges
1 Answer
Reset to default 4Here is the solution. First save the selected image in one of your state variable. For example create a state variable called "selectedImage" as below:
const [selectedImage, setSelectedImage] = useState(null);
Now, when you select image set selected image in your state variable like this:
ImagePicker.launchImageLibrary(options, (response) => {
if (response.uri) {
setSelectedImage(response);
}
});
Now create FormData
as below:
const datas = new FormData();
datas.append('images', {
name: selectedImage.fileName,
type: selectedImage.type,
uri:
Platform.OS === 'android' ? selectedImage.uri : selectedImage.uri.replace('file://', ''),
});
And pass as below :
axios({
method: "POST",
url: API_NO_PARAM_CONFIG.createCampaign,
headers: {
Authorization: `Bearer ${e}`,
"Content-Type": "multipart/form-data", // add this
},
datas, //pass datas directly
});
本文标签: javascriptHow To Upload Image from ReactNativeImagePicker with axiosStack Overflow
版权声明:本文标题:javascript - How To Upload Image from React-Native-Image-Picker with axios? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745481294a2660183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论