微信小程序文件上傳,圖片上傳

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');

},

})

}

})

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章