MCPcopy Create free account
hub / github.com/baiwumm/react-admin / UploadImage

Function UploadImage

Xmw_web/src/components/UploadImage/index.tsx:36–178  ·  view source on GitHub ↗
({
  value,
  onChange,
  type = 'upload',
  maxSize = 5, // 图片大小,默认5m
  fieldProps,
  imgCropProps = {
    rotationSlider: true,
    cropShape: 'round',
    showGrid: true,
  },
  ...uploadProps
})

Source from the content-addressed store, hash-verified

34} & (ProFormUploadButtonProps | ProFormUploadDraggerProps);
35
36const UploadImage: FC<UploadImageProps> = ({
37 value,
38 onChange,
39 type = 'upload',
40 maxSize = 5, // 图片大小,默认5m
41 fieldProps,
42 imgCropProps = {
43 rotationSlider: true,
44 cropShape: 'round',
45 showGrid: true,
46 },
47 ...uploadProps
48}) => {
49 // 多语言函数
50 const { formatMessage } = useIntl();
51 // hooks 调用
52 const { message } = App.useApp();
53 // 获取 Token
54 const ACCESS_TOKEN = getLocalStorageItem<string>(LOCAL_STORAGE.ACCESS_TOKEN);
55 // 上传图片loading
56 const [uploadLoading, { setTrue: setUploadLoadingTrue, setFalse: setUploadLoadingFalse }] =
57 useBoolean(false);
58 // 文件列表
59 const [fileList, setFileList] = useState<UploadFile[]>([]);
60 // 上传回调
61 const onChangeUpload: UploadProps['onChange'] = ({
62 file,
63 fileList,
64 }: UploadChangeParam<UploadFile>) => {
65 // 移除图片清空值
66 if (file.status === 'removed') {
67 onChange?.(undefined);
68 } else {
69 // 上传中
70 if (file.status === 'uploading') {
71 setUploadLoadingTrue();
72 }
73 // 上传完成
74 if (file.status === 'done') {
75 // 获取当前上传图片路径
76 const path: string = get(file, 'response.data.path');
77 setUploadLoadingFalse();
78 onChange?.(path);
79 }
80 }
81 setFileList(fileList);
82 };
83
84 /**
85 * @description: 限制用户上传的图片格式和大小
86 * @param {RcFile} file
87 * @author: 白雾茫茫丶
88 */
89 const beforeUpload = (file: RcFile) => {
90 // 获取限制的图片类型,默认全部
91 const accept = get(fieldProps, 'accept');
92 // 判断类型是否正确
93 const isFileType = !accept || accept.includes(file.type);

Callers

nothing calls this directly

Calls 3

getLocalStorageItemFunction · 0.90
isHttpLinkFunction · 0.90
renderUploadDomFunction · 0.85

Tested by

no test coverage detected