antd中Upload上传图片宽高限制以及上传文件的格式限制

项目中有一个需求,要上传轮播图,且有尺寸要求,所以就需要在上传图片的时候进行尺寸限制,使用了Upload组件,需要在组件的beforeUpload方法中进行限制。

定义一个上传前的方法,并且添加一个图片尺寸获取的方法:

如果尺寸不符合要求,validateImageSize方法会返回false,如果尺寸符合就会返回true

// Carousel upload handle
    const carouselUpload: any = async (file: any) => {
        console.log('only limit 750x420', file)
        const limitSize = await validateImageSize(file)
        if (!limitSize) {
            console.log('only limit error', file)
            message.error('图片尺寸不符合要求,请重新上传')
        }
        return limitSize || Upload.LIST_IGNORE
    }

    const validateImageSize = (file: any) => {
        return new Promise((resolve, reject) => {
            const reader = new FileReader()
            reader.onload = (e: any) => {
                const image = new Image()
                image.onload = function () {
                    const { width, height } = this as any
                    console.log('image size is width  height', width, height)
                    if (width === 150 && height === 150) {
                        resolve(true)
                    } else {
                        resolve(false)
                    }
                }
                image.onerror = reject
                image.src = e.target.result
            }
            reader.onerror = reject
            reader.readAsDataURL(file)
        })
    }

组件中添加使用:

                    <Upload
                        action="https://dev.hado-official.cn/service_api/file_upload"
                        accept="image/*"
                        fileList={fileList}
                        maxCount={3}
                        beforeUpload={carouselUpload}
                        onChange={onChange}
                        onPreview={onPreview}
                    >
                        {fileList.length < 5 && (
                            <div className="uploadBox">
                                <span className="uploadBtn">点击上传</span>
                                <span className="uploadTips">
                                    最多3张,单张图片不超过10MB,尺寸750*420px,支持jpg/png/bmp格式
                                </span>
                            </div>
                        )}
                    </Upload>

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 19:58:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 19:58:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 19:58:01       82 阅读
  4. Python语言-面向对象

    2024-04-23 19:58:01       91 阅读

热门阅读

  1. Qt实现XYModem协议(七)

    2024-04-23 19:58:01       37 阅读
  2. 数据库服务器NTP调整

    2024-04-23 19:58:01       36 阅读
  3. Python网络爬虫项目开发实战:如何处理并发下载

    2024-04-23 19:58:01       39 阅读
  4. LC343. 整数拆分

    2024-04-23 19:58:01       34 阅读
  5. c++ primer plus(1)

    2024-04-23 19:58:01       34 阅读
  6. Unity学习笔记之——PlayerPrefs

    2024-04-23 19:58:01       35 阅读
  7. git小白教程

    2024-04-23 19:58:01       36 阅读
  8. 数据库表按月进行分区

    2024-04-23 19:58:01       34 阅读