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

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

对上传文件进行限制,只能是 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>
				</>
			)
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python可以通过使用不同的库来实现各类文件格式之间的转换。以下是一些常用的库和示例代码: 1. 将文本文件转换为PDF: ```python from fpdf import FPDF def text_to_pdf(input_file, output_file): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) with open(input_file, "r") as file: for line in file: pdf.cell(0, 10, txt=line, ln=True) pdf.output(output_file) input_file = "input.txt" output_file = "output.pdf" text_to_pdf(input_file, output_file) ``` 2. 将Word文档转换为PDF: ```python from docx2pdf import convert input_file = "input.docx" output_file = "output.pdf" convert(input_file, output_file) ``` 3. 将图片文件转换为PDF: ```python from PIL import Image def images_to_pdf(input_files, output_file): images = [] for file in input_files: image = Image.open(file) images.append(image.convert("RGB")) images.save(output_file, save_all=True, append_images=images[1:]) input_files = ["image1.jpg", "image2.png"] output_file = "output.pdf" images_to_pdf(input_files, output_file) ``` 4. 将Excel文件转换为PDF: ```python import pandas as pd from fpdf import FPDF def excel_to_pdf(input_file, output_file): df = pd.read_excel(input_file) table = df.to_html(index=False) pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.write_html(table) pdf.output(output_file) input_file = "input.xlsx" output_file = "output.pdf" excel_to_pdf(input_file, output_file) ``` 请根据你的需求选择适合的库和代码进行文件格式转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值