element-ui的upload限制图片的格式,大小,以及宽高

<div >不多于5张图片,600*600px,png、jpg或gif格式,大小不超过2M</div>
                    <el-upload action="https://jsonplaceholder.typicode.com/posts/"
                               list-type="picture-card"
                               multiple
                               :limit="5"
                               :before-upload="brforeUpload" 
                               :on-preview="handlePictureCardPreview"
                               :on-remove="handleRemove"
                               >
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%"
                             :src="dialogImageUrl"
                             alt="">
                    </el-dialog>
brforeUpload(file) {
            let bool = true;
            let isSize = getImgInfo(file);
            isSize.catch(() => {
                this.$message({
                    message: '注意:文件格式为600*600!',
                    type: 'warning'
                });
            })
            let fileType = ['.png', '.jpg', '.gif'];
            let { name, size } = file;
            let hzIndex = name.lastIndexOf('.');
            let nameLen = name.length;
            //获取后缀名
            let hz = name.substring(hzIndex, nameLen);
            //文件大小不得大于2M
            let imgSize = (parseFloat(size) / 1024 / 1024) > 2;
            if (!fileType.includes(hz) || imgSize) {
                this.$message({
                    message: '注意:文件格式不正确!',
                    type: 'warning'
                });
                bool = false;
            }
            return isSize || bool;

        }

------>封装的getImgInfo
/**
 * @description 得到图片的基本信息
 * @param{object} file 文件
 * @param{number} _width 默认是100px
 * @param{number} _height 默认是100px
 */
export const getImgInfo = (file, _width = 100, _height = 100) => {
    const isSize = new Promise(function (resolve, reject) {
        let width = _width;
        let height = _height;
        //Window.URL 属性返回一个对象,它提供了用于创建和管理对象URLs的静态方法
        let _URL = window.URL || window.webkitURL;
        let img = new Image();
        img.onload = () => {
            let valid = img.width <= width && img.height <= height;
            valid ? resolve() : reject();
        }
        //https://developer.mozilla.org/zh-CN/docs/Web/API/URL/createObjectURL  具体查看
        img.src = _URL.createObjectURL(file);
    }).then(() => {
        return file;
    }, () => {
        return Promise.reject();
    })
    return isSize;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
您好,关于 element-ui 的上传组件 el-upload,您可以通过自定义 el-upload-list 来实现自定义上传列表的功能。 具体实现方法如下: 1. 在 el-upload 组件中添加属性 list-type="picture-card",并设置属性 action 为上传文件的地址。 2. 在 el-upload 组件中添加自定义 slot,用于自定义上传列表的显示。 3. 在自定义的上传列表组件中,使用 el-upload-list 组件来展示上传文件的列表。 4. 可以通过监听 el-upload 组件的 change 事件来获取上传的文件信息,并将上传的文件信息传递给自定义的上传列表组件,以展示上传文件的列表。 以下是一个简单的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="/upload" list-type="picture-card" :on-change="handleChange" > <i class="el-icon-plus"></i> <div class="upload-text">上传图片</div> </el-upload> <custom-upload-list :file-list="fileList"></custom-upload-list> </div> </template> <script> import CustomUploadList from './CustomUploadList.vue'; export default { components: { CustomUploadList, }, data() { return { fileList: [], }; }, methods: { handleChange(file, fileList) { this.fileList = fileList; }, }, }; </script> ``` CustomUploadList.vue 组件的代码如下: ```html <template> <el-upload-list class="custom-upload-list" :file-list="fileList"> <template slot-scope="{file}"> <div class="custom-list-item"> <img :src="file.url" class="custom-list-item-thumbnail"> <div class="custom-list-item-name">{{ file.name }}</div> <div class="custom-list-item-actions"> <el-button size="small" type="text" @click="handleRemove(file)">删除</el-button> </div> </div> </template> </el-upload-list> </template> <script> export default { props: { fileList: { type: Array, default: () => [], }, }, methods: { handleRemove(file) { const index = this.fileList.indexOf(file); if (index !== -1) { this.fileList.splice(index, 1); } }, }, }; </script> ``` 希望这个示例能够帮助到您。如果您有任何问题,请随时提出。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开发路上的AZhe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值