index.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import SystemConst from '@/utils/const';
  2. import Token from '@/utils/token';
  3. import { useState } from 'react';
  4. import { connect } from '@formily/react';
  5. import { Button, Input, message, Spin, Upload } from 'antd';
  6. import type { UploadChangeParam } from 'antd/lib/upload/interface';
  7. interface Props {
  8. value: string;
  9. onChange: (value: string) => void;
  10. accept?: string;
  11. }
  12. const FileUpload = connect((props: Props) => {
  13. const [url, setUrl] = useState<string>(props?.value);
  14. const [loading, setLoading] = useState<boolean>(false);
  15. const handleChange = (info: UploadChangeParam) => {
  16. setLoading(true);
  17. if (info.file.status === 'done') {
  18. message.success('上传成功!');
  19. info.file.url = info.file.response?.result;
  20. setUrl(info.file.response?.result);
  21. setLoading(false);
  22. props.onChange(info.file.response?.result);
  23. }
  24. };
  25. return (
  26. <Spin spinning={loading}>
  27. <Upload
  28. accept={props?.accept || '*'}
  29. listType={'text'}
  30. action={`/${SystemConst.API_BASE}/file/static`}
  31. headers={{
  32. 'X-Access-Token': Token.get(),
  33. }}
  34. onChange={handleChange}
  35. showUploadList={false}
  36. >
  37. <Input.Group compact>
  38. <Input
  39. style={{ width: 'calc(100% - 100px)' }}
  40. value={url}
  41. onClick={(e) => {
  42. e.preventDefault();
  43. e.stopPropagation();
  44. }}
  45. placeholder="请上传文件"
  46. />
  47. <Button shape="round" style={{ width: '100px', textAlign: 'center' }} type="primary">
  48. 上传jar包
  49. </Button>
  50. </Input.Group>
  51. </Upload>
  52. </Spin>
  53. );
  54. });
  55. export default FileUpload;