1. 程式人生 > >微信小程式上傳檔案詳解

微信小程式上傳檔案詳解

做微信小程式難免會遇到上傳檔案的問題。今天就給大家說一個簡單的上傳檔案的例子吧
wxml程式碼

<button bindtap="upload">上傳檔案</button>

js程式碼

Page({
  data:{
    path:''
  },
  upload:function(){
        var that=this
        wx.chooseImage({
        count: 1, 
        sizeType: ['original', 'compressed'], 
        sourceType: ['album'
, 'camera'], success: function (res) { var tempFilePaths = res.tempFilePaths console.log(tempFilePaths) wx.uploadFile({ url: 'http://example.weixin.qq.com/upload', filePath: tempFilePaths[0], name: 'file'
, formData:{ 'user': 'test' }, success: function(res){ var data = res.data wx.showModal({ title: '上傳檔案返回狀態', content: '成功', success: function
(res) {
if (res.confirm) { console.log('使用者點選確定') } } }) //do something }, fail:function(res){ console.log(res) } }) that.setData({ path:tempFilePaths }) } }) } })

button 按鈕也可以換成其他的標籤,視情況而定,另外我這裡加了我自己的返回提示,你們也可以取消,不懂的,可以評論,或者掃描下方二維碼
這裡寫圖片描述