How to limit the size of WeChat applet when uploading images (with code)

不言
Release: 2018-08-10 17:15:38
Original
15015 people have browsed it

The content of this article is about how to limit the size of WeChat applet when uploading pictures (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Recently, there was a WeChat applet project. The requirement was to upload multiple pictures, and the number could not exceed 3. Moreover, in php, because the picture upload was set to only upload 2M, the upload was unsuccessful, so I Set up a function on the front end and use the WeChat API (wx.chooseImage) to obtain pictures. If the picture is larger than 3, a prompt will pop up and you cannot continue to upload the picture. If the picture is larger than 2M, a prompt will pop up. You can only upload 2M. For the following pictures,

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;   
             })

        }

}
Copy after login

I use the size in tempFiles to determine the image size. The minimum version 1.2.0 is supported

Related recommendations:

Mini Program: How to dynamically add and delete JSON object arrays (with code)

WeChat Mini Program Example: How to call Tencent Map to obtain jsonp data

How to call API to implement data request in WeChat applet

The above is the detailed content of How to limit the size of WeChat applet when uploading images (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template