当前位置:网站首页>antd upload beforeUpload中禁止触发onchange
antd upload beforeUpload中禁止触发onchange
2022-07-06 09:28:00 【梦想身高1米8】
官网:https://ant.design/components/upload-cn/#components-upload-demo-upload-png-only
稍微加点注释:beforeUpload属性返回false或Promise.reject,只会阻止上传(不会触发上传的请求),但是onchange函数还是会触发的,想要阻止onchange函数,需要beforeUpload返回Upload.LIST_IGNORE
1 返回false或Promise.reject
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/antd/4.20.6/antd.min.js" integrity="sha512-Pn2PLpM5OByFVBBfe517wK9LQCCbQsM4sVl7y3yHDCpHL/X/k6st3cre9SUiopZrbBiOQiSDgvKMOagM6MWCnA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/antd/4.20.6/antd.min.css" integrity="sha512-GKJ7hcKFof/Chhk79mhxA8egInBxwypeeD9sF/fAlTJ75n0YvjQ8mSofBD7KahRnMTzdtUFF5+YWjnXTew8Y0w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const custom = () => {
const [number, setNumber] = React.useState(0); const beforeUpload = file => {
const isPNG = file.type == 'image/png'; return isPNG; // return new Promise(()=>{
// const isPNG = file.type == 'image/png'; // if(isPNG) {
// resolve(); // } // else {
// reject(); // } // }) } const onChange = info => {
antd.message.info('触发onchange') if (info.file.status == 'uploading') {
antd.message.info('正在上传') } if (info.file.status === 'done') {
antd.message.info('上传成功') } else if (info.file.status === 'error') {
antd.message.info('上传失败') } } return <antd.Upload name='file' action='https://www.mocky.io/v2/5cc8019d300000980a055e76' headers={
{
authorization: 'authorization-text', }} onChange={
info => onChange(info)} beforeUpload={
beforeUpload} > <antd.Button>上传</antd.Button> </antd.Upload > } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>
2 返回Upload.LIST_IGNORE
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/antd/4.20.6/antd.min.js" integrity="sha512-Pn2PLpM5OByFVBBfe517wK9LQCCbQsM4sVl7y3yHDCpHL/X/k6st3cre9SUiopZrbBiOQiSDgvKMOagM6MWCnA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/antd/4.20.6/antd.min.css" integrity="sha512-GKJ7hcKFof/Chhk79mhxA8egInBxwypeeD9sF/fAlTJ75n0YvjQ8mSofBD7KahRnMTzdtUFF5+YWjnXTew8Y0w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const custom = () => {
const [number, setNumber] = React.useState(0); const beforeUpload = file => {
const isPNG = file.type == 'image/png'; return isPNG || antd.Upload.LIST_IGNORE; // return new Promise(()=>{
// const isPNG = file.type == 'image/png'; // if(isPNG) {
// resolve(); // } // else {
// return antd.Upload.LIST_IGNORE; // } // }) } const onChange = info => {
antd.message.info('触发onchange') if (info.file.status == 'uploading') {
antd.message.info('正在上传') } if (info.file.status === 'done') {
antd.message.info('上传成功') } else if (info.file.status === 'error') {
antd.message.info('上传失败') } } return <antd.Upload name='file' action='https://www.mocky.io/v2/5cc8019d300000980a055e76' headers={
{
authorization: 'authorization-text', }} onChange={
info => onChange(info)} beforeUpload={
beforeUpload} > <antd.Button>上传</antd.Button> </antd.Upload > } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>
我试了一下,如果使用promise,如果不返回resove()
或reject()
(啥返回都没有的情况),onchange也不会触发。
ps:网页里使用react和antd的情况,可以看这个
https://blog.csdn.net/weixin_43915401/article/details/125046690
边栏推荐
- pytorch提取骨架(可微)
- “鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
- [exercise-4] (UVA 11988) broken keyboard = = (linked list)
- If you want to apply for a programmer, your resume should be written like this [essence summary]
- C language must memorize code Encyclopedia
- 读取和保存zarr文件
- 树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
- (POJ - 3579) median (two points)
- The "sneaky" new asteroid will pass the earth safely this week: how to watch it
- 1005. Maximized array sum after K negations
猜你喜欢
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
Quick to typescript Guide
b站 实时弹幕和历史弹幕 Protobuf 格式解析
1855. Maximum distance of subscript alignment
Advancedinstaller安装包自定义操作打开文件
The concept of C language array
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
[exercise-5] (UVA 839) not so mobile (balance)
1903. Maximum odd number in string
随机推荐
Web based photo digital printing website
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
渗透测试 ( 4 ) --- Meterpreter 命令详解
921. Minimum additions to make parentheses valid
树莓派4B安装opencv3.4.0
Penetration test (7) -- vulnerability scanning tool Nessus
pytorch提取骨架(可微)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
【练习-7】Crossword Answers
[exercise-3] (UVA 442) matrix chain multiplication
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
Is the sanic asynchronous framework really so strong? Find truth in practice
860. Lemonade change
Sanic异步框架真的这么强吗?实践中找真理
Truck History
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
969. Pancake sorting
MySQL grants the user the operation permission of the specified content
Pytorch extract skeleton (differentiable)
X-forwarded-for details, how to get the client IP