|
@@ -1,64 +1,85 @@
|
1
|
|
-import React, { useCallback, useState } from 'react'
|
2
|
|
-import { UploadOutlined } from '@ant-design/icons';
|
3
|
|
-import { Upload, Button } from 'antd';
|
4
|
|
-import { uploadFile } from '@/services/rotationChart';
|
|
1
|
+import React, { useCallback, useState } from "react";
|
|
2
|
+import { UploadOutlined } from "@ant-design/icons";
|
|
3
|
+import { Upload, Button } from "antd";
|
|
4
|
+import { uploadFile } from "@/services/rotationChart";
|
5
|
5
|
|
6
|
6
|
const customRequest = ({ file, onError, onSuccess }) => {
|
7
|
7
|
const data = new FormData();
|
8
|
|
- data.append('file', file);
|
|
8
|
+ data.append("file", file);
|
9
|
9
|
|
10
|
|
- uploadFile(data).then((res) => {
|
11
|
|
- onSuccess(res.url)
|
12
|
|
- }).catch((err) => {
|
13
|
|
- onError(err);
|
14
|
|
- })
|
|
10
|
+ uploadFile(data)
|
|
11
|
+ .then((res) => {
|
|
12
|
+ onSuccess(res.url);
|
|
13
|
+ })
|
|
14
|
+ .catch((err) => {
|
|
15
|
+ onError(err);
|
|
16
|
+ });
|
15
|
17
|
|
16
|
18
|
return {
|
17
|
19
|
abort() {
|
18
|
|
- console.log('upload progress is aborted.');
|
|
20
|
+ console.log("upload progress is aborted.");
|
19
|
21
|
},
|
20
|
22
|
};
|
21
|
|
-}
|
|
23
|
+};
|
22
|
24
|
|
23
|
25
|
export default (props) => {
|
24
|
26
|
const { value, onChange, preview, ...inputProps } = props;
|
25
|
27
|
const [loading, setLoading] = useState(false);
|
26
|
28
|
|
27
|
|
- const onUpload = useCallback((e) => {
|
28
|
|
- const { files } = e.target;
|
29
|
|
- if (!files || files.length < 1) return;
|
30
|
|
-
|
31
|
|
- setLoading(true);
|
32
|
|
- const data = new FormData();
|
33
|
|
- data.append('file', files[0]);
|
|
29
|
+ const onUpload = useCallback(
|
|
30
|
+ (e) => {
|
|
31
|
+ const { files } = e.target;
|
|
32
|
+ if (!files || files.length < 1) return;
|
34
|
33
|
|
35
|
|
- uploadFile(data).then((res) => {
|
36
|
|
- setLoading(false);
|
37
|
|
- if (typeof onChange === 'function') {
|
38
|
|
- onChange(res.url)
|
39
|
|
- }
|
40
|
|
- }).catch((err) => {
|
41
|
|
- setLoading(false);
|
42
|
|
- // onError(err);
|
43
|
|
- })
|
|
34
|
+ setLoading(true);
|
|
35
|
+ const data = new FormData();
|
|
36
|
+ data.append("file", files[0]);
|
44
|
37
|
|
45
|
|
- }, [onChange])
|
|
38
|
+ uploadFile(data)
|
|
39
|
+ .then((res) => {
|
|
40
|
+ setLoading(false);
|
|
41
|
+ if (typeof onChange === "function") {
|
|
42
|
+ onChange(res.url);
|
|
43
|
+ }
|
|
44
|
+ })
|
|
45
|
+ .catch((err) => {
|
|
46
|
+ setLoading(false);
|
|
47
|
+ // onError(err);
|
|
48
|
+ });
|
|
49
|
+ },
|
|
50
|
+ [onChange]
|
|
51
|
+ );
|
46
|
52
|
|
47
|
53
|
const onClick = () => {
|
48
|
|
- const input = document.createElement('input');
|
49
|
|
- input.type = 'file';
|
50
|
|
- Object.assign(input, inputProps);
|
51
|
|
- input.addEventListener('change', onUpload);
|
|
54
|
+ const input = document.createElement("input");
|
|
55
|
+ input.type = "file";
|
|
56
|
+ Object.assign(input, inputProps);
|
|
57
|
+ input.addEventListener("change", onUpload);
|
52
|
58
|
// input.removeEventListener('change', onUpload);
|
53
|
59
|
input.click();
|
54
|
|
- }
|
|
60
|
+ };
|
55
|
61
|
|
56
|
62
|
return (
|
57
|
63
|
<div>
|
58
|
|
- <Button icon={<UploadOutlined />} onClick={onClick}>上传文件</Button>
|
59
|
|
- {
|
60
|
|
- value && typeof preview === 'function' && preview(value)
|
61
|
|
- }
|
|
64
|
+ <Button icon={<UploadOutlined />} onClick={onClick}>
|
|
65
|
+ 上传文件
|
|
66
|
+ </Button>
|
|
67
|
+ {value &&
|
|
68
|
+ preview &&
|
|
69
|
+ (typeof preview === "function" ? (
|
|
70
|
+ preview(value)
|
|
71
|
+ ) : (
|
|
72
|
+ <div style={{ marginTop: "1em" }}>
|
|
73
|
+ <a
|
|
74
|
+ href={`${value}`}
|
|
75
|
+ download
|
|
76
|
+ // rel="noopener noreferrer"
|
|
77
|
+ // key="link"
|
|
78
|
+ >
|
|
79
|
+ {value?.substring(value?.lastIndexOf("/") + 1)}
|
|
80
|
+ </a>
|
|
81
|
+ </div>
|
|
82
|
+ ))}
|
62
|
83
|
</div>
|
63
|
|
- )
|
64
|
|
-}
|
|
84
|
+ );
|
|
85
|
+};
|