微信小程序开发中怎样实现图片上传

网友投稿 689 2022-10-07 17:07:02

微信小程序开发中怎样实现图片上传

老规矩,直接上代码了。

handleCancelPic() { let id = this.data.dbId; wx.chooseImage({ count: 3, // 默认9 sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: res => { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths; this.setData({ src: tempFilePaths }) upload(this,tempFilePaths,'',''); } }) }然后一个封装好的方法function upload(page, path,way,id) { console.log(path) wx.showToast({ icon: "loading", title: "正在上传" }); var test = [], that = this; for (var i = 0; i wx.uploadFile({ url: api.CancelImg, filePath: path[i], name: 'file', header: { "Content-Type": "multipart/form-data" }, success: res => { test.push(res); wx.setStorageSync('cancelImg',test) console.log(test) if (res.statusCode != 200) { wx.showModal({ title: '提示', content: '上传失败', showCancel: false }) return; }else { wx.showModal({ title: '提示', content: '上传成功', showCancel: false }) } }, fail: function (e) { console.log(e); wx.showModal({ title: '提示', content: '上传失败', showCancel: false }) }, complete: function () { wx.hideToast(); //隐藏Toast } }) }登录后复制

这个是多个图片上传的方法,单个图片上传的话,把循环去掉就好。主要是因为微信官方默认的就是一次上传一张图片这个很蛋疼。只能这么搞了。。。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:jquery的then方法
下一篇:解决预装win8系统重装win7后无法进入系统
相关文章