您好,欢迎访问一九零五行业门户网

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

老规矩,直接上代码了。
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<path.length; 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
}
})
}
这个是多个图片上传的方法,单个图片上传的话,把循环去掉就好。主要是因为微信官方默认的就是一次上传一张图片这个很蛋疼。只能这么搞了。。。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
微信小程序开发容器视图的使用
微信小程序开发中android请求失败如何处理
以上就是微信小程序开发中怎样实现图片上传的详细内容。
其它类似信息

推荐信息