我一直试图从react-native发布一个图像文件,但似乎无法正确获取
这是我尝试的代码:
const pickImage = async () => {
// No permissions request is necessary for launching the image library
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
if (!result.canceled){
delete result.cancelled;
setImage(result.assets[0].uri)
}
};
字符串
这是fetch请求:
const handleSubmit = (name, description, image, email, phone) => {
let uri = image.replace("///", "//")
var myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${userInfo.access_token}`);
myHeaders.append("Content-Type", "multipart/form-data");
var formdata = new FormData();
formdata.append("name", name);
formdata.append("description", description);
formdata.append("photo", uri, uri.split("/").pop());
formdata.append("phone", phone);
formdata.append("email", email);
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: formdata,
redirect: 'follow'
};
fetch(`${Url.base}/api/realtor/create-profile`, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
型
这是我经常遇到的错误:
{“photo”:[“提交的数据不是文件。请检查表单上的编码类型。"]}
1条答案
按热度按时间ee7vknir1#
文件对象应该有
uri
、name
和type
字段,类似于:字符串