成都小程序开发,如何上传图片?
打开开发者工具,新建小程序项目
打开新建好的小程序,index.wxml 添加一个按钮 ,加入按下事件
打开index.js加入uploadpic方法
修改图片上传接口。
具体js代码:
uploadpic: function () {
let that = this;
wx.showActionSheet({
itemList: ['从相册中选择', '拍照'],
itemColor: "#f7982a",
success: function (res) {
if (!res.cancel) {
if (res.tapIndex == 0) {
that.chooseWxImage('album')
} else if (res.tapIndex == 1) {
that.chooseWxImage('camera')
}
}
}
})
},
chooseWxImage: function (type) {
var that = this;
wx.chooseImage({
sizeType: ['original', 'compressed'],
sourceType: [type],
success: function (ress) {
wx.showToast({
icon: "loading",
title: "正在上传"
}),
wx.uploadFile({
url: app.siteurl + '/index.php?m=api&c=index&v=uploadpic',
filePath: ress.tempFilePaths[0],
name: 'file',
header: { "Content-Type": "multipart/form-data" },
formData: {
'appkey': app.appkey
},
success: function (res) {
if (res.statusCode != 200) {
wx.showModal({
title: '提示',
content: '上传失败',
showCancel: false
})
return;
}
},
fail: function (e) {
wx.showModal({
title: '提示',
content: '上传失败',
showCancel: false
})
},
complete: function () {
wx.hideToast();
}
})
}
})
},
如果您希望通过成都小程序开发大程度地提高业绩,成都创新互联可以为您提供帮助。