思路:先选择微信小程序聊天记录里面的文件实现上传,代码如下
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)
}
})
}
}
})
},
注意点:
- count:最多可以选择的文件个数,可以 0~100
- 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