封装—小程序上传图片,限制图片的格式和大小

  • Post author:
  • Post category:小程序


export default function(type, callBack) {

let isUpload = true

return new Promise((resolve, reject) => {

wx.chooseImage({

count: 1,

success: res => {

let types = res.tempFiles[0].path.substring(res.tempFiles[0].path.length – 3);

if (types == ‘jpg’ || types == ‘png’) {

if (res.tempFiles[0].size > 1024 * 1024 * 10) {

wx.showToast({

title: ‘图片请控制在10Mb以内’, //标题

icon: ‘none’ //图标 none不使用图标,详情看官方文档

})

return;

}

const tempFilePaths = res.tempFilePaths[0];

resolve(isUpload ? uploadImg(tempFilePaths, type, callBack).catch(() => { reject() }) : tempFilePaths)

} else {

wx.showToast({

title: ‘只支持jpeg/jpg/png格式’, //标题

icon: ‘none’ //图标 none不使用图标,详情看官方文档

})

return;

}

},

fail: res => {

reject()

},

complete: () => {}

})

})

}



版权声明:本文为m0_56804390原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。