四川鑫光豪耀专注四川成都网站设计 成都网站制作 成都网站建设
四川成都网站建设公司服务热线: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://www.rksyc.com/view/178346.html

其他资讯