layui 控制上传图片大小的方法

layui 控制上传图片大小的方法

layui.use(['upload', 'form'], function () {            var upload = layui.upload;            var form = layui.form;
            upload.render({
                elem: '#uploadCover'
                , url: '/file/upload.shtml'
                , method: "post"
                , auto: false  //auto 参数必须设置为false
                ,size:"2048"
                ,accept:"file"
                ,exts: 'jpg|png|jpeg'
                ,choose: function(obj){  //上传前选择回调方法                    var flag = true;
                    obj.preview(function(index, file, result){
                        console.log(file);            //file表示文件信息,result表示文件src地址                        var img = new Image();
                        img.src = result; 
                        img.onload = function () { //初始化夹在完成后获取上传图片宽高,判断限制上传图片的大小。                            if(img.width ==343 && img.height ==240){
                                obj.upload(index, file); //满足条件调用上传方法
                            }else{
                                flag = false;
                                D.msg("您上传的小图大小必须是343*240尺寸!");                                return false;
                            }
                        }                        return flag;
                    });
                }
                , done: function (res) {//上传成功回调方法
                    $("#xcCoverUrl").val(res.data);
                    $("#xcCoverUrlShow").attr("src", res.fastdfsHost + res.data);
                    layer.closeAll('loading');
                }
            });
        });

复制代码


写的好!

微信扫一扫赏大洋