微信小程序上传文件详解

做微信小程序难免会遇到上传文件的问题。今天就给大家说一个简单的上传文件的例子吧
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 按钮也可以换成其他的标签,视情况而定,另外我这里加了我自己的返回提示,你们也可以取消,不懂的,可以评论,或者扫描下方二维码
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_35730500/article/details/53639079