微信小程序拍照上传图片或头像
在使用微信小程序时需要用到手机拍照来上传图片或者头像时
在method中定义一个方法获取图片路径,将路径赋值到image中
方法如下:
takePhoto() {const ctx = wx.createCameraContext();ctx.takePhoto({quality: 'high', //画质高success: (res) => {var img = res.tempImagePath;let token = util.getStorageSync(token) //将token传入上传图片接口中wx.uploadFile({url: url, //上传接口路径filePath: img,name: 'files',formData: {'token': token},success: function (res) {if (res.flag) {wx.showToast({title: "上传图片失败",icon: 'none',duration: 2000});return;}var ims = JSON.parse(res.data);var tempFilePaths = ims.data.image;util.setStorageSync("qrcodeImg", tempFilePaths)that.setData({src: tempFilePaths})}})}, fail(res) {console.log(res);}})},