Home  >  Article  >  WeChat Applet  >  Analysis of an example of limiting the size of multiple images uploaded by WeChat applet

Analysis of an example of limiting the size of multiple images uploaded by WeChat applet

php是最好的语言
php是最好的语言Original
2018-08-07 10:38:097160browse

Recently I am working on a WeChat applet project. The purpose is to upload multiple pictures. The number cannot exceed 3. Moreover, in PHP, because the picture upload is set to only upload 2M, the upload is unsuccessful, so I set it on the front end. A function that uses the WeChat API (wx.chooseImage) to obtain pictures. If the picture is larger than 3, a prompt will pop up and the picture cannot be uploaded. If the picture is larger than 2M, a prompt will pop up. Only pictures under 2M can be uploaded. ,

data:{
    img:[], //设置一个数组
}

add_img:function(){
    var that = this,
    img = that.data.img;
        if(img.length < 3){  //如果图片数量小于3张,可以直接获取图片
            wx.chooseImage({
                count:1,     //默认9
                sizeType:[&#39;compressed&#39;], //可以指定原图还是压缩图,默认二者都有
                sourceType:[&#39;album&#39;,&#39;camera&#39;],//可以指定来源相册还是相机,默认二者都有
                success:function(res){
                    var tempFilesSize = res.tempFiles[0].size;  //获取图片的大小,单位B
                    if(tempFilesSize <= 2000000){   //图片小于或者等于2M时 可以执行获取图片
                        var tempFilePaths = res.tempFilePaths[0]; //获取图片
                        that.data.img.push(tempFilePaths);   //添加到数组
                        that.setData({
                            img:that.data.img
                        })
                    }else{    //图片大于2M,弹出一个提示框
                        wx.showToast({
                            title:&#39;上传图片不能大于2M!&#39;,  //标题
                            icon:&#39;none&#39;       //图标 none不使用图标,详情看官方文档
                        })
                    }
                }
            })
        }else{  //大于三张时直接弹出一个提示框
             wx.showToast({
                 title:&#39;上传图片不能大于3张!&#39;,
                 icon:&#39;none&#39;   
             })

        }

}

Analysis of an example of limiting the size of multiple images uploaded by WeChat applet

I use the size in tempFiles to determine the image size. The minimum version supported is 1.2.0 (I don’t understand, I know where this 1.2.0 is from) Version? WeChat version or WeChat applet development tool version? Comment if you know, thank you)

Related articles:

Example detailed explanation of WeChat applet uploading pictures to the server

Implementation example code of multiple image uploads in WeChat Mini Program

Related videos:

Comprehensive and in-depth analysis video tutorial of WeChat Mini Program

The above is the detailed content of Analysis of an example of limiting the size of multiple images uploaded by WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn