微信小程序上传文件详解

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

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值