js 上传图片限制分辨率

本文介绍如何使用JavaScript在用户上传图片前进行分辨率检查,确保上传的图片符合指定要求,减少服务器处理负担并提高用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

imgChange() {
            if (event.target.files.length == 0) {
                return;
            } else {
                let that = this;
                var mapPictureSize = [];

                let file = event.target.files[0];

                var reader = new FileReader;

                reader.onload = function (evt) {

                    var image = new Image();

                    image.onload = function () {

                        mapPictureSize[0] = this.width + "";
                        mapPictureSize[1] = this.height + "";

                        console.log(mapPictureSize);

                        let size = 500 * 1024;
                        if (file.size > size) {
                            that.$refs.imgupload.value = '';
                            return that.uploadStatus = true;
                        }
                        if (mapPictureSize[0] != 800 && mapPictureSize[1] != 600) {
                            that.$refs.imgupload.value = '';
                            return that.uploadStatus = true;
                        }
                        that.uploadStatus = false;
                        // console.log(file)
                        let fromdata = new FormData();
                        fromdata.append('files', file);
                        uploadFile(fromdata).then((res) => {
                            // console.log(res);
                            that.info.coverUrl = res.result[0].fileId;
                        })

                        that.$refs.imgupload.value = '';
                    };
                    image.src = evt.target.result;
                };
                reader.readAsDataURL(file);

            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值