{...UploadProps} 只能上传JPG, PNG ,Word,Xlsx或Pdf 文件

该代码段展示了一个React组件的配置,用于限制上传的文件类型只能是JPG,PNG,Word文档,Excel的Xlsx格式或Pdf文件,并且检查文件大小必须小于3MB。如果文件类型或大小不符合要求,用户会收到错误消息。上传成功后,文件路径会被存储并可以用于下载或回显。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对上传文件进行限制,只能是 JPG, PNG ,Word,Xlsx或Pdf 文件

上传  下载  回显

 

 

	const UploadProps: UploadProps = {
		name: 'file',
		listType: 'text',
		action: `${environment.baseUrl}/api/Files/UploadFile/EmailAttach`,
		headers: {
			Authorization: getAuthority(),
		},
		maxCount: 1,
		beforeUpload(file) {

			const isJpgOrPng =
				file.type === 'image/jpeg' ||
				file.type === 'image/png' ||
				file.type === 'application/pdf' ||
				file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
				file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
				;
			if (!isJpgOrPng) {
				message.error('附件只能是 JPG, PNG ,Word,Xlsx或Pdf 文件!');
				return Upload.LIST_IGNORE;
			}
			const isLt2M = file.size / 1024 / 1024 < 3;
			if (!isLt2M) {
				message.error('文件大小必须小于3MB!');
				return Upload.LIST_IGNORE;
			}
			return isJpgOrPng && isLt2M;
		},

		onChange(info) {
			if (info.file.status === 'uploading') {
				return;
			}
			if (info.file.status === 'done') {
				setAttachFilePath(info.file.response.ResData);
			}

		},
	};
const UploadBtns = () => {
		if (dataItem?.AppendixUrl) {
			return (
				<>
					<div className="appendix_url">
						<a href={`${baseUrl}${dataItem?.AppendixUrl}`} target="_blank">
							<Tooltip title="Click to download the file">
								<div style={{ width: '10%' }}>
									<img src={wordImg} alt="" />
									<span>Sick leave</span>
								</div>
							</Tooltip>
						</a>
					</div>
				</>
			)
		} else if (!dataItem?.AppendixUrl && !action) {
			return (
				<>
					<div className="upload_btn">
						<Form.Item name="AppendixUrl"
							rules={[
								{
									required: true,
									message: 'Please upload the resignation letter',
								}
							]}
						>
							<Upload {...UploadProps}>
								<Button
									className="add_appendix"
									icon={<PlusOutlined />}
									size="large"
								>
									Add appendix
								</Button>
							</Upload>
						</Form.Item>
					</div>
				</>
			)
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值