gpt4 book ai didi

reactjs - React AntDesign 将上传的图像添加到 FormData

转载 作者:行者123 更新时间:2023-12-03 13:41:33 25 4
gpt4 key购买 nike

我想使用image uploader from AntDesign library 。这是snapshot

import { Upload, Icon, Modal } from 'antd'

class PicturesWall extends React.Component {
state = {
previewVisible: false,
previewImage: '',
fileList: [],
}

handleCancel = () => this.setState({ previewVisible: false })

handlePreview = file => {
this.setState({
previewImage: file.url || file.thumbUrl,
previewVisible: true,
})
}

handleChange = ({ fileList }) => this.setState({ fileList })

render() {
const { previewVisible, previewImage, fileList } = this.state
const uploadButton = (
<div>
<Icon type="plus" />
<div className="ant-upload-text">Upload</div>
</div>
)
return (
<div className="clearfix">
<Upload
action="//jsonplaceholder.typicode.com/posts/"
listType="picture-card"
fileList={fileList}
onPreview={this.handlePreview}
onChange={this.handleChange}
>
{fileList.length >= 3 ? null : uploadButton}
</Upload>
<Modal
visible={previewVisible}
footer={null}
onCancel={this.handleCancel}
>
<img alt="example" style={{ width: '100%' }} src={previewImage} />
</Modal>
</div>
)
}
}

ReactDOM.render(<PicturesWall />, mountNode)

我很难理解这里发生了什么。我可以使用类似的东西从这个组件获取图像吗const img=event.target.files[0];

我想要的只是将上传的图像放入数组中,并使用 FormData 将 axios.post 请求发送到后端。我是 React 的新手。如果这是显而易见的事情,请原谅我。提前谢谢您

最佳答案

antdUpload 组件正在后台为您执行上传操作。但如果您不想这样做,并稍后上传文件,您也可以借助 beforeUpload 属性来实现。

From the docs :

beforeUpload: Hook function which will be executed before uploading. Uploading will be stopped with false or a rejected Promise returned. Warning:this function is not supported in IE9

我编写了一个示例并在必要时添加了注释:

class PicturesWall extends React.Component {
state = {
previewVisible: false,
previewImage: "",
fileList: []
};

handleCancel = () => this.setState({ previewVisible: false });

handlePreview = file => {
this.setState({
previewImage: file.thumbUrl,
previewVisible: true
});
};

handleUpload = ({ fileList }) => {
//---------------^^^^^----------------
// this is equivalent to your "const img = event.target.files[0]"
// here, antd is giving you an array of files, just like event.target.files
// but the structure is a bit different that the original file
// the original file is located at the `originFileObj` key of each of this files
// so `event.target.files[0]` is actually fileList[0].originFileObj
console.log('fileList', fileList);

// you store them in state, so that you can make a http req with them later
this.setState({ fileList });
};

handleSubmit = event => {
event.preventDefault();

let formData = new FormData();
// add one or more of your files in FormData
// again, the original file is located at the `originFileObj` key
formData.append("file", this.state.fileList[0].originFileObj);

axios
.post("http://api.foo.com/bar", formData)
.then(res => {
console.log("res", res);
})
.catch(err => {
console.log("err", err);
});
};

render() {
const { previewVisible, previewImage, fileList } = this.state;
const uploadButton = (
<div>
<Icon type="plus" />
<div className="ant-upload-text">Upload</div>
</div>
);
return (
<div>
<Upload
listType="picture-card"
fileList={fileList}
onPreview={this.handlePreview}
onChange={this.handleUpload}
beforeUpload={() => false} // return false so that antd doesn't upload the picture right away
>
{uploadButton}
</Upload>

<Button onClick={this.handleSubmit} // this button click will trigger the manual upload
>
Submit
</Button>

<Modal
visible={previewVisible}
footer={null}
onCancel={this.handleCancel}
>
<img alt="example" style={{ width: "100%" }} src={previewImage} />
</Modal>
</div>
);
}
}

ReactDOM.render(<PicturesWall />, document.getElementById("container"));

关于reactjs - React AntDesign 将上传的图像添加到 FormData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54845951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com