我是靠谱客的博主 鳗鱼蚂蚁,这篇文章主要介绍react antd上传图片视频pdf,现在分享给大家,希望可以做个参考。

antd上传附件分为图片视频文件,我封装成一个组件供使用,以下有几个案例,根据如下进行匹配:
组件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import './index.less'; import { FileTextOutlined, PlusOutlined } from '@ant-design/icons'; import { useMemoizedFn } from 'ahooks'; import { Empty, Image, message, Upload, UploadProps } from 'antd'; import { RcFile } from 'antd/lib/upload'; import { UploadFile } from 'antd/lib/upload/interface'; import deploy from 'config'; import _ from 'lodash'; import React, { useState } from 'react'; import { getToken } from 'utils'; interface Props extends UploadProps { fileType?: 'img' | 'office' | 'pdf'; } const index: React.FC<Props> = React.memo(({ className, fileType, ...props }) => { const [previewUrl, setPreviewUrl] = useState<string | undefined>(); const handleBeforeUpload = useMemoizedFn((file: RcFile, FileList: RcFile[]) => { if (fileType === 'pdf') { if (!file.type.includes('application/pdf')) { message.info({ content: '请正确的选择PDF', className: 'ant-message-custom no-icon' }); return Upload.LIST_IGNORE; } } if (fileType === 'img') { if (!file.type.includes('image/')) { message.info({ content: '请正确的选择图片', className: 'ant-message-custom no-icon' }); return Upload.LIST_IGNORE; } } if (fileType === 'office') { if (!file.type.includes('application/vnd.openxmlformats') && !file.type.includes('application/vnd.ms')) { message.info({ content: '请正确的选择文档', className: 'ant-message-custom no-icon' }); return Upload.LIST_IGNORE; } } return true; }); const handlePreview = useMemoizedFn(async (file: UploadFile<any>) => { if (file.preview) { setPreviewUrl(file.preview); } else if (file.thumbUrl) { setPreviewUrl(file.thumbUrl); } else if (file.originFileObj) { let previewUrl = file.preview ?? (await getBase64(file.originFileObj)); if (previewUrl) setPreviewUrl(previewUrl as string); } }); return ( <> <Upload className={`ant-upload-file ${className ?? ''}`} action={`${deploy.SERVER_URL}/datafile/upload`} headers={{ Authorization: `${getToken()}` }} listType="picture-card" {..._.omit(props, ['value', 'defaultValue'])} onChange={(info) => { const { file } = info; if (file.error) { file.error = { statusText: '上传失败', message: '上传失败', }; } if (file.response?.message && typeof file.response?.message === 'string') { file.error = { statusText: file.response?.message, message: file.response?.message, }; message.error(file.response?.message); file.response.message = undefined; } props.onChange && props.onChange(info); }} onPreview={props.onPreview ?? handlePreview} beforeUpload={fileType ? handleBeforeUpload : props.beforeUpload} > {props.children ?? ( <div className="ant-upload-button-card"> <PlusOutlined /> <div style={{ marginTop: 8 }}>上传</div> </div> )} </Upload> <span style={{ display: 'none' }}> <Image preview={{ src: previewUrl, visible: Boolean(previewUrl), onVisibleChange: () => setPreviewUrl(undefined) }} /> </span> </> ); }); /// get Img file Base64 function getBase64(file: RcFile) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = (error) => reject(error); }); } export default index; export const FileSelectBox: React.FC<{ image?: React.ReactNode; description?: string; height?: string | number; width?: string | number; style?: React.CSSProperties; }> = React.memo((props) => { return ( <div className="ant-upload-file-slect-box" style={{ width: props.width, height: props.height, ...props.style }}> <Empty image={props.image ?? <FileTextOutlined />} imageStyle={{ height: 20 }} description={props.description ?? '选择文件'} /> </div> ); });

使用图片:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<FileUpload fileType="img" style={{ width: '100%' }} maxCount={3} defaultFileList={props?.record?.imageList.map((e) => ({ uid: e.id, name: e.name, fileName: e.name, status: 'done', type: 'image/jpeg', response: { id: e.id }, thumbUrl: `${deploy.SERVER_URL}/datafile/download?dataFileId=${e.id}&token=${getToken()}`, }))} onChange={(info) => { let ids: string[] = info.fileList.filter((o) => o.status === 'done').map((o) => o.response['id']); props.form.setFieldsValue({ images: ids }); }} children={ <div className="ant-upload-button-card"> <FileJpgOutlined style={{ fontSize: 40 }} /> <div style={{ marginTop: 8 }}>选择图片</div> </div> } />

使用pdf:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<FileUpload fileType="pdf" style={{ width: '100%' }} showUploadList={{ showPreviewIcon: false, }} defaultFileList={props?.record?.constructionDrawingsList.map((e) => ({ uid: e.id, name: e.name, fileName: e.name, status: 'done', type: 'application/pdf', response: { id: e.id }, }))} onChange={(info) => { let ids: string[] = info.fileList.filter((o) => o.status === 'done').map((o) => o.response['id']); props.form.setFieldsValue({ constructionDrawings: ids }); }} children={ <div className="ant-upload-button-card"> <FilePdfOutlined style={{ fontSize: 40 }} /> <div style={{ marginTop: 8 }}>选择PDF</div> </div> } />

视频上传也如上述如此。

最后

以上就是鳗鱼蚂蚁最近收集整理的关于react antd上传图片视频pdf的全部内容,更多相关react内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部