【发布时间】:2015-08-24 13:51:00
【问题描述】:
我想将图像从我的应用上传到 S3 服务器。我计算了所有数据和代码(在计算机上使用 curl 测试),但我无法弄清楚如何正确调用“获取”。我得到了回应:
'您指定的前提条件中至少有一个不成立。条件:bucket POST 必须是enclosure-type-multipart/form-data'
如何在 react-natives fetch 中重新创建表单数据?没有我可以附加的 FormData,然后像 fetches 示例中那样发送它。
编辑: 感谢@philipp-von-weitershausen,感谢您添加了此功能。但是我在调用它时遇到了一些麻烦。我得到“不支持的 BodyInit 类型”。发现是因为在 fetch.js: "support.formData" 返回 false。当我调用 fetch 时,我错过了什么?
我的代码示例:
var form = new FormData();
form.append("FormData", true)
form.append("name", this.state.invoiceNumber)
form.append("key", this.state.invoiceNumber)
form.append("Content-Type", "image/png")
form.append('file', this.props.uri)
//alert(FormData.prototype.isPrototypeOf(form))
fetch(amazon_url,{body: form,mode: "FormData", method: "post", headers: {"Content-Type": "multipart/FormData"}})
.then((response) => response.json())
.catch((error) => {
alert("ERROR " + error)
})
.then((responseData) => {
alert("Succes "+ responseData)
})
.done();
【问题讨论】:
-
您有最终工作代码的示例吗?我也遇到了麻烦。
-
事实上,你有没有一个例子可以发布表单变量(没有图像 - 看到下一个 RN 构建添加图像支持)。
-
react-native-fetch-blob 可以为您做到这一点。
标签: javascript file-upload amazon-s3 fetch react-native