如何在 react-native 中使用 fetch 发布 multipart/formdata? [英] How to post multipart/formdata using fetch in react-native?
本文介绍了如何在 react-native 中使用 fetch 发布 multipart/formdata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想发布这样的表单数据.
i want to post Form Data like that.
我应该为发送图像文件数据做些什么准备?
what should i prepare for sending image file data?
我有 Uri、类型、文件名、大小.
i have Uri, type, filename, size.
然后将使用 fetch .
then will use fetch for it.
标题中的内容类型是'multipart/formdata'
Content-type in header is 'multipart/formdata'
感谢帮助
推荐答案
你应该有一个上传功能,应该是这样的:
You should have an upload function, which should look like this:
upload(url, data) {
let options = {
headers: {
'Content-Type': 'multipart/form-data'
},
method: 'POST'
};
options.body = new FormData();
for (let key in data) {
options.body.append(key, data[key]);
}
return fetch(requestUrl, options)
.then(response => {
return response.json()
.then(responseJson => {
//You put some checks here
return responseJson;
});
});
}
你这样称呼它,发送图像 blob 路径:
And you call it this way, sending the image blob path:
this.upload('http://exampleurl.com/someApiCall', {
file: {
uri: image.path,
type: image.mime,
name: image.name,
}
}).then(r => {
//do something with `r`
});
这篇关于如何在 react-native 中使用 fetch 发布 multipart/formdata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文