028-86922220
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

成都小程序开发,如何上传图片?

成都小程序开发,如何上传图片?


打开开发者工具,新建小程序项目


打开新建好的小程序,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();


}


})


}


})


},


如果您希望通过成都小程序开发大程度地提高业绩,成都创新互联可以为您提供帮助。


分享名称:成都小程序开发,如何上传图片?
本文网址:http://whjierui.cn/view/178649.html

其他资讯