【发布时间】:2021-08-18 21:50:15
【问题描述】:
为了不浪费你的时间,我尽量缩短代码和解释,
首先,一切正常……在哪里 在前端,照片选择器工作并已成功尝试。 在后端,Post API 正在运行并由 Postman 通过从数据上传文件进行测试,它给了我 200 个状态代码
但是当我发布图像时问题就出现了,但是使用React Native Android emulator时出现了这个错误
登录后台终端是 “错误:多部分:未找到边界”
这是代码,
import axios from 'axios';
import React, {useState} from 'react';
import {Button, View, Image} from 'react-native';
import ImagePicker from 'react-native-image-crop-picker';
function ImageScreen() {
const [image, setImage] = useState('');
// I think the error is here
const postData = () => {
axios
.post(
'http://10.0.2.2:3000/posts',
{
name: 'new post', // هنا اختصرت الكود حفظا للوقت
image: image,
},
{
headers: {
'Content-Type': `multipart/form-data`,
},
},
)
.then(res => console.log(res.data))
.catch(err => console.log(err));
};
// That works fine I tested it,
const choosePhotoFromLibrary = () => {
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: true,
compressImageQuality: 0.7,
}).then(image => {
console.log(image);
setImage(image.path);
this.bs.current.snapTo(1);
});
console.log(image);
};
return (
<View>
<Image style={{height: 300, width: 300}} source={{uri: image}} />
<Button title="from device" onPress={choosePhotoFromLibrary} />
<Button title="Post Data" onPress={postData} />
</View>
);
}
export default ImageScreen;
【问题讨论】:
-
axios 返回一个你应该使用
thencatch或async和await的承诺 -
我成功了,但是出现了新的错误,
-
后台终端登录为“Error: Multipart: Boundary not found”
-
你能和我们分享一下实际的例子吗?据我在这个例子中看到的问题是你没有提供一个有效的标题,在表单数据中你应该发送边界标题。
标签: reactjs react-native http axios multipartform-data