UploadExcel.jsx 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React, { useState } from 'react';
  2. import { LoadingOutlined, UploadOutlined } from '@ant-design/icons';
  3. import { Button, message, Upload } from 'antd';
  4. import { saveImportPerson } from '@/services/invoicePerson'
  5. function beforeUpload(file) {
  6. const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  7. if (!isExcel) {
  8. message.error('请上传excle表格(务必通过模板下载修改)');
  9. }
  10. const isLt10M = file.size / 1024 / 1024 < 10;
  11. if (!isLt10M) {
  12. message.error('文件大小必须小于 10MB!');
  13. }
  14. return isExcel && isLt10M;
  15. }
  16. export default (props) => {
  17. const { onChange, id, refrash } = props;
  18. const [loading, setLoading] = useState(false);
  19. const handleChange = info => {
  20. if (info.file.status === 'uploading') {
  21. setLoading(true);
  22. return;
  23. }
  24. if (info.file.status === 'error') {
  25. setLoading(false);
  26. refrash()
  27. return;
  28. }
  29. if (info.file.status === 'done') {
  30. setLoading(false);
  31. onChange(info.file.response);
  32. }
  33. };
  34. function uploadFile({ file, onSuccess, onError }) {
  35. const formData = new FormData();
  36. formData.append('file', file);
  37. saveImportPerson(formData, id).then((url) => {
  38. onSuccess(url, file)
  39. }).catch((e) => {
  40. onError(e)
  41. });
  42. return {
  43. abort: () => { },
  44. };
  45. }
  46. return (
  47. <Upload
  48. customRequest={uploadFile}
  49. showUploadList={false}
  50. beforeUpload={beforeUpload}
  51. onChange={handleChange}
  52. >
  53. <Button type="primary" disabled={loading} icon={loading ?
  54. <LoadingOutlined /> : <UploadOutlined />}>上传</Button>
  55. </Upload>
  56. );
  57. }