Home > Article > WeChat Applet > How to limit the size of WeChat applet when uploading images (with code)
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:['compressed'], //可以指定原图还是压缩图,默认二者都有 sourceType:['album','camera'],//可以指定来源相册还是相机,默认二者都有 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:'上传图片不能大于2M!', //标题 icon:'none' //图标 none不使用图标,详情看官方文档 }) } } }) }else{ //大于三张时直接弹出一个提示框 wx.showToast({ title:'上传图片不能大于3张!', icon:'none' }) } }
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!