【发布时间】:2018-02-13 06:10:37
【问题描述】:
我正在尝试使用 XML api 上传到谷歌云存储。这是用于执行此操作的示例表单。 感谢布兰登·亚伯勒
<form action="http://my-bucket.storage.googleapis.com" method="post" enctype="multipart/form-data">
<input type="text" name="key" value="">
<input type="hidden" name="signature" value="sample_token=">
<input name="file" type="file">
<input type="submit" value="Upload">
</form>
我也在使用 ant design Upload 组件来做到这一点。
文档指示使用“customRequest”属性来实现您自己的 XMLHttpRequest。此外,它还链接到描述“customRequest”接收的对象的更多文档,我在下面将其解构为参数。
我已经尝试实现了。
render() {
const props = {
name: 'file',
action: '//localhost:3000/my-express-endpoint',
data: {
'key': 'value'
},
customRequest({ onProgress, onError, onSuccess, data, filename, file, withCredentials, action, headers }) {
let data = new FormData(); // Using FormData counting on browser support.
data.append('signature', 'sample_token');
let xhr = new XMLHttpRequest();
xhr.open('POST', 'http://my-bucket.storage.googleapis.com', true);
xhr.onload = function () {
// do something to response
console.log(this.responseText);
};
xhr.send(data);
}
};
return (
<div className="container">
<Dragger {...props}>
...drop file here
</Dragger>
</div>
)
}
我不清楚如何执行 customRequest。由于我正在覆盖,这是否会使 customRequest 道具之外的其他道具 name, action, data 无效,并且现在在 customRequest 道具中处理,因为它们是相同的?另外,由于我使用 XML api 直接上传到谷歌云存储,这是否意味着我不再需要使用 my-express-endpoint 来处理它并使用例如服务器端的 multer 来处理它?
【问题讨论】:
标签: javascript reactjs google-cloud-storage antd