springboot---微信小程序上传文件(word/pdf文件)

springboot后台

C:\Users\Lenovo\Desktop\各种框架安装包\demo-uploadfile
(随便一个上传文件的java接口)

wx.js
chose() {
    var serverUrl = "http://localhost:8080/upload"
    var that = this
    console.log('111')
    wx.chooseMessageFile({
      count: 1, //能选择文件的数量
      type: 'file', //能选择文件的类型,我这里只允许上传文件.还有视频,图片,或者都可以
      success(res) {
        console.log(res.tempFiles[0])
        var size = res.tempFiles[0].size;
        var filename = res.tempFiles[0].name;
        var newfilename = filename + "";
        //  ||newfilename.indexOf(".pdf")==-1
        if (size > 4194304) { //我还限制了文件的大小和具体文件类型
          wx.showToast({
            title: '文件大小不能超过4MB,格式必须为pdf!',
            icon: "none",
            duration: 2000,
            mask: true
          })
        } else {
          console.log(res.tempFiles[0].path, filename)
          that.setData({
            path: res.tempFiles[0].path, //将文件的路径保存在页面的变量上,方便 wx.uploadFile调用
            filename: filename //渲染到wxml方便用户知道自己选择了什么文件
          })
          wx.uploadFile({
            url: serverUrl, //上传的路径
            filePath: that.data.path, //刚刚在data保存的文件路径
            name: 'file', //后台获取的凭据
            success() {
              wx.showToast({ //做个提示或者别的操作
                title: '上传成功',
                icon: "none",
                duration: 5000,
                mask: true,
                success: function (res) {

                }
              })
            }
          })
        }
      }
    })

  },
wx.ml
<form bindsubmit="chose">
     <input name="firstname" value="请输入姓名" />
       <input name="lastname" value="请输入姓名" />
 <button formType="submit">提交</button>
    
    </form>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值