微信小程式檔案上傳,圖片上傳
阿新 • • 發佈:2018-12-16
1:xml
<view style="margin-top: 20px" class="weui-flex"> <view style="width: 35%;font-size: 0.7em">證件照片</view> <!-- <input class="_input weui-flex__item" value="{{appkey}}" /> --> <view class="weui-flex__item"> <image src='{{developer_file}}' style='width:100px;height:100px'></image> </view> <view style="display:{{isSave == 'show' ? 'none':'block'}}"> <button size='mini' bindtap="btn_img">選擇照片</button> </view> </view>
2:js
btn_img: function (e) { //圖片上傳 var that = this; wx.chooseImage({ count: 1, // 預設9 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相簿還是相機,預設二者都有 success: function (res) { var tempFilePaths = res.tempFilePaths; wx.uploadFile({ url: '', //此處換上你的介面地址 filePath: tempFilePaths[0], name: 'file', header: { }, formData: { 'user': 'test', 'file': tempFilePaths[0] }, success: function (res) { if (res.statusCode == '200') { console.log("圖片上傳成功"); var jsonData = JSON.parse(res.data); that.setData({ imageFile: tempFilePaths, developer_file: jsonData.data }) } else { showSuccess('上傳失敗'); } }, fail: function (res) { console.log('fail'); }, }) } }) }