【发布时间】:2021-08-10 13:17:34
【问题描述】:
我正在使用 React,我正在尝试上传一个文件,该文件类型应该只是 png。 当我上传 png 文件时,它工作正常。但我需要停止上传其他类型的文件。例如,如果我需要上传 png 文件意味着它需要正常工作。如果我错误地上传音频文件意味着该文件不应该被上传。请告诉我如何编写这样的验证。
这是我的代码 这是 App.js
import React, { useState } from "react";
import 'antd/dist/antd.css';
import './index.css';
import { Row, Col, Button, Modal, Upload, message } from 'antd';
import { VideoCameraOutlined, AudioOutlined } from '@ant-design/icons';
import "./App.css";
const App = () => {
const props = {
beforeUpload: file => {
const audioValidation = file.type === "image/png"
if (!audioValidation) {
message.error('You can only upload PNG file!');
}
}
}
const [visible, setVisible] = useState(false)
const showPopUp = () => {
setVisible(true)
}
return (
<div>
<Row>
<Col span={24}>
<Button onClick={() => showPopUp()} type="primary">Show PopUp</Button>
<Modal
visible={visible}
>
<Upload {...props}>
<div style={{ display: "flex" }}>
<div>
<VideoCameraOutlined style={{ fontSize: "25px", backgroundColor: "red", padding: "10px", borderRadius: "50%" }} />
<h6>Upload Video</h6>
</div>
<div style={{ marginLeft: "5px" }}>
<AudioOutlined style={{ fontSize: "25px", backgroundColor: "red", padding: "10px", borderRadius: "50%" }} />
<h6>Upload Png</h6>
</div>
</div>
</Upload>
</Modal>
</Col>
</Row>
</div>
)
}
export default App
如果您有任何问题,请告诉我,谢谢。
【问题讨论】:
-
您已经在检查文件类型。那有什么问题呢?
-
@Alen.Toma 问题在于文件提示实际上并没有限制用户提交扩展名不同于
.png的文件。