1. 程式人生 > >小程序之圖片上傳

小程序之圖片上傳

span let clas stat path .get can pan gets

//調用相冊等選擇圖片,得到圖片的相對路勁
up_img: function () { var that = this; wx.chooseImage({ count: 1, // 默認9 sizeType: [‘compressed‘], sourceType: [‘album‘, ‘camera‘], success: function (res) { var tempFilePaths = res.tempFilePaths; upload(that, tempFilePaths); } }) }
//把圖片上傳到服務器
function
upload(page, path) { wx.showToast({ icon: "loading", title: "正在上傳" }), wx.uploadFile({ url: app.globalData.url + ‘index.php?g=api&m=api&a=img_upload_api‘, filePath: path[0], name: ‘file‘, formData: { openid: wx.getStorageSync(
‘openid‘), is_type: 1 }, success: function (res) { if (res.statusCode != 200) { wx.showModal({ title: ‘提示‘, content: ‘上傳失敗‘, showCancel: false })
return; } var res_data = JSON.parse(res.data); // poster_src = res_data.data; page.setData({ poster_src: res_data.data }); }, fail: function (e) { console.log(e); wx.showModal({ title: ‘提示‘, content: ‘上傳失敗‘, showCancel: false }) }, complete: function () { wx.hideToast(); //隱藏Toast } }); }

小程序之圖片上傳