• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

Next
Prev

微信小程序如何上传word,excel文件

Data: 2015-02-18 08:08:42Form: JournalClick: 10

微信小程序如何上传word,excel文件

思路:先选择微信小程序聊天记录里面的文件实现上传,代码如下

 chooseUpload() {
    var that = this
    wx.chooseMessageFile({
      count: 10,
      type: 'file',
      extension: ['.xlsx', '.xls', '.XLSX', '.XLS', 'xlsx', 'xls', 'XLSX', 'XLS'],
      success(res) {
        const tempFilePaths = res.tempFiles
        for (var i in tempFilePaths) {
          wx.uploadFile({
            url: 'http://xxx', //上传的服务器地址
            filePath: tempFilePaths[i].path,
            name: 'file',
            formData: {
              'file': tempFilePaths[i].path
                   },

            header: {
              [wx.getStorageSync('tokenName')]: wx.getStorageSync('token'),
            },
            success: function (resp) {
              console.log(resp)
              var data = JSON.parse(resp.data)
              console.log(data)
              if (data.code == 200) {
                wx.showToast({
                  title: '上传成功',
                  icon: 'none',
                  duration: 1300
                })
              } else {
                wx.showToast({
                  title: data.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            },
            fail: function (err) {
              console.log(err)
            }
          })
        }
      }
    })
  },

注意点:

  1. count:最多可以选择的文件个数,可以 0~100
  2. type所选择文件的类型
合法值说明
all从所有文件选择
video只能选择视频文件
image只能选择图片文件
file可以选择除了图片和视频之外的其它的文件

3. extension:根据文件拓展名过滤,仅 type==file 时有效。每一项都不能是空字符串。默认不过滤。(在ios系统下文件拓展名前不需要加 . )

4. filePath:要上传文件资源的路径 (本地路径)

5.name:文件对应的名字 key

6. header:HTTP 请求 Header,Header 中不能设置 Referer

7. formData:HTTP 请求中其他额外的 form data

Name:
<提交>