Judging the file type and size before layui file upload callback

1. Regarding layui.upload, before uploading, the file type and file size are judged, and the default upload implementation method is prevented [the code in the following picture is for the width and height limit processing of image upload]:

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 parameter must be set to false 
                ,size: "2048" 
                ,accept: "file" 
                ,exts: 'jpg|png|jpeg' 
                ,choose: function (obj){ //Select callback method before uploading
                     var flag = true ;
                    obj.preview(function(index, file, result){
                        console.log(file); //file represents file information, result represents file src address
                        var img = new Image();
                        img.src = result; 
                        img.onload = function () { //The initialization folder obtains the width and height of the uploaded image after the initialization is completed, and determines the size of the upload image.
                            if (img.width ==343 && img.height ==240 ){
                                obj.upload(index, file); //Call the upload method when the conditions are met
                            }else{
                                flag = false;
                                D.msg( "The size of the thumbnail you upload must be 343*240!" );
                                 return  false ;
                            }
                        }
                        return flag;
                    });
                }
                , done: function (res) {// Upload success callback method 
                    $( "#xcCoverUrl" ).val(res.data);
                    $("#xcCoverUrlShow").attr("src", res.fastdfsHost + res.data);
                    layer.closeAll('loading');
                }
            });
        });

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607037&siteId=291194637