知与行后台管理端

ImageUpload.jsx 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React from 'react';
  2. import { Upload, Icon, message } from 'antd';
  3. import './style.less';
  4. import { uploaderProps } from '../../utils/upload';
  5. class ImageUpload extends React.Component {
  6. state = {
  7. loading: false,
  8. imageUrl: undefined,
  9. };
  10. handleChange = info => {
  11. if (info.file.status === "uploading") {
  12. this.setState({ loading: true });
  13. return;
  14. }
  15. if (info.file.status === "done") {
  16. this.setState({
  17. loading: false,
  18. })
  19. if (info.file.response && info.file.response.url) {
  20. this.setState({
  21. imageUrl: info.file.response.thumbUrl,
  22. });
  23. if (typeof this.props.onChange === 'function') {
  24. this.props.onChange(info.file.response.url);
  25. }
  26. }
  27. }
  28. };
  29. render() {
  30. const uploadButton = (
  31. <div>
  32. <Icon style={{ fontSize: '2em', color: '#aaa' }} type={this.state.loading ? "loading" : "plus"} />
  33. </div>
  34. );
  35. const value = this.props.value;
  36. return (
  37. <Upload
  38. listType="picture-card"
  39. className="avatar-uploader"
  40. showUploadList={false}
  41. beforeUpload={this.props.beforeUpload}
  42. onChange={this.handleChange}
  43. {...uploaderProps}
  44. >
  45. {(this.state.imageUrl || value) ? (
  46. <img src={this.state.imageUrl || value} alt="avatar" style={{ width: "100%" }} />
  47. ) : (
  48. uploadButton
  49. )}
  50. </Upload>
  51. );
  52. }
  53. }
  54. export default ImageUpload;