Javascript 如何将映像和阵列与react native一起发布到后端?
我正在尝试使用react native将图像文件和数据(对象或数组)发送到后端。我尝试使用fetch和下面的代码,并让它将图像发送到后端。但我的目标是将图像和数据一起发送。我怎样才能做到这一点Javascript 如何将映像和阵列与react native一起发布到后端?,javascript,reactjs,react-native,post,fetch,Javascript,Reactjs,React Native,Post,Fetch,我正在尝试使用react native将图像文件和数据(对象或数组)发送到后端。我尝试使用fetch和下面的代码,并让它将图像发送到后端。但我的目标是将图像和数据一起发送。我怎样才能做到这一点 uploadPhoto = async (response) => { const formData = new FormData(); formData.append('fileToUpload', { uri: response.path, n
uploadPhoto = async (response) => {
const formData = new FormData();
formData.append('fileToUpload', {
uri: response.path,
name: 'image',
type: response.mime,
imgPath: response.path
});
const infos={ad:'onur',soyad:'cicek'};
try {
const rest = await fetch('https://www.birdpx.com/mobile/fotografyukle/'+this.state.user.Id, {
method: 'POST',
headers: { 'Accept': 'application/json', 'Content-type': 'multipart/form-data' },
body: formData
});
const gelenVeri = await rest.text();
let convertedVeri=JSON.parse(gelenVeri);
console.log(convertedVeri);
return false
} catch (err) {
console.log(err)
}
};
我需要发布
constinfos={ad:'onur',soyad:'cicek}代码>和图像。您可以在formData本身中传递它
formData.append('other_infos', infos);
这里的other\u infos
将是从后端提取所需的关键,您可以在FormData
中附加n个参数。您可以通过向FormData添加其他数据来完成此操作。
就像这个例子一样
formData.append('infos', infos);