成都小程序开发之图片上传-大悟县灿优商贸网站建设

关于灿优商贸

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

成都小程序开发之图片上传

成都小程序开发中需要用到的图片上传功能详解:



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


打开新建好的小程序,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.apyobr.com/view/178346.html

其他资讯