antDesign中Upload组件文件上传超出大小不能阻止上传行为

今天遇到的问题:

当前用的Ant Design Vue的2.x版本,Upload组件。根据API中展示,beforeUpload返回false就能阻止上传动作,经实验得出提示的同时,文件还能继续上传。


问题代码如下:

<template>
....
<a-upload    
      :multiple="true"
      v-model:fileList="fileList"      
      :customRequest="upload"
      @download="handleDownload"
      :showUploadList="false"
      :beforeUpload="beforeUpload"
    >
  <a-button>
    <upload-outlined></upload-outlined>
    上传
  </a-button>
</a-upload>
......
</template>
<script lang="ts">
  import { UploadOutlined } from '@ant-design/icons-vue';
  import { defineComponent, ref, getCurrentInstance } from 'vue';

  interface FileItem {
    id: string;
    name: string;
    url: string;
  }

  export default defineComponent({
	name:"UploadFile",
	components: {
	  UploadOutlined,
	},
	props:{
		fileList:{type:Array<FileItem>},
		maxFileSize:Number//maxFileSize以M为单位
	},
	setup(props,ctx) {
       const fileList = ref<FileItem[]>(props.fileList);
        .......
        //此处省略很多代码
        .......
		const upload=(options)=>{
			//图片上传ajax方法
		}
		const beforeUpload = (f, list)=>{
			const isLt50M = f.size / 1024 / 1024 < props.maxFileSize
		    if(!isLt50M) {
				this.$message.error(f.name + '文件大小超出限制,请修改后重新上传')
				return false
			} else {
				return true
			}
		}
		return {
			upload,
			fileList,
			handleDownload,
			deleteHandle,
			beforeUpload
		}
	
	}
  });
</script>

网络上其他解决方案:

beforeUpload 有个坑:

       必须要返回一个 Promise,promise 中 必须返回false 才能阻止上传,通过校验必须返回true和 resolve 否则会阻止上传。

修改后的代码如下:

const beforeUpload = (f, list)=>{
    return new Promise((resolve)=>{
            const isLt50M = f.size / 1024 / 1024 < props.maxFileSize
            if(!isLt50M) {
                this.$message.error(f.name + '文件大小超出限制,请修改后重新上传')
                return false
            }
            fileList.value = [...fileList.value, f];
          resolve(true);
          return true;
     });
}

这种方法,最后isLt50M为true的时候,也就是正常要求能上传文件的时候,fileList中没新上传的文件显示出来。估计问题出在fileList赋值上面。

其实beforeUpload 的第二个参数list就是fileList,当提交前文件列表都在这里存放着呢,如果大于fileSize了,直接把其pop删除第一个即可。不用return Promise了。也可以return Promise 然后处理fileList.value那句赋值就行。两种方案都可以。

修改第一种的解决办法是:

const beforeUpload = (fileItem, fList)=>{
    const isLt50M = fileItem.size / 1024 / 1024 < props.maxFileSize
    if(!isLt50M) {
      proxy.$message.error(fileItem.name + '文件大小超出限制'+props.maxFileSize+'M,请重新上传')
      fList.pop();//其实fList就是fileList,需要的是这步操作,而不是new Promise
      return false
    } else {
       return true
    }
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant DesignUpload组件可以通过设置`showUploadList`属性为`false`,然后使用自定义的进度条组件来实现上传进度的显示。 以下是一个上传大文件并显示进度条进度的示例代码: ```jsx import { Upload, Button } from 'antd'; import { UploadOutlined } from '@ant-design/icons'; import React, { useState } from 'react'; const UploadProgress = ({ percent }) => ( <div style={{ margin: '10px 0' }}> <div style={{ width: `${percent}%`, height: '5px', backgroundColor: '#1890ff' }}></div> </div> ); const Demo = () => { const [uploading, setUploading] = useState(false); const [progress, setProgress] = useState(0); const handleUpload = ({ file }) => { const formData = new FormData(); formData.append('file', file); setUploading(true); // 模拟上传进度 const timer = setInterval(() => { setProgress((prevProgress) => { if (prevProgress >= 100) { clearInterval(timer); setUploading(false); return 100; } else { return prevProgress + 10; } }); }, 500); // 发送上传请求 // axios.post('/api/upload', formData) // .then(() => { // clearInterval(timer); // setUploading(false); // }) // .catch(() => { // clearInterval(timer); // setUploading(false); // }); }; return ( <Upload name="file" action="/api/upload" showUploadList={false} beforeUpload={() => false} onChange={() => {}} customRequest={handleUpload} > <Button icon={<UploadOutlined />} disabled={uploading}> {uploading ? '上传' : '点击上传'} </Button> {uploading && <UploadProgress percent={progress} />} </Upload> ); }; export default Demo; ``` 这段代码,我们定义了一个`UploadProgress`组件作为自定义的进度条组件,它接受一个`percent`属性用来表示上传进度的百分比。在`handleUpload`函数,我们使用`setInterval`模拟上传进度,并使用`setProgress`函数更新上传进度。当上传进度达到100时,我们清除定时器并将`uploading`状态设置为`false`,表示上传完成。在`Upload`组件,我们将`showUploadList`属性设置为`false`,禁用默认的上传列表,然后使用自定义的按钮和进度条组件来替代默认的上传按钮和上传进度条。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值