Home>Article>WeChat Applet> How to upload pictures in WeChat applet development
The old rule is to go directly to the code.
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
}
})
}
This is a method for uploading multiple pictures. If you want to upload a single picture, just remove the loop. Mainly because WeChat’s official default is to upload one picture at a time, which is a pain in the ass. This is all we can do. . .
# I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The use of container views in WeChat applet development
How to handle Android request failure in WeChat applet development
The above is the detailed content of How to upload pictures in WeChat applet development. For more information, please follow other related articles on the PHP Chinese website!