样式啥的就略过吧,使用uni.chooseMessageFile方法,直接上代码吧
data() {
return {
fileList:[], //传给服务器
file:[], //传给后端,提交接口使用
}
},
// 上传pdf 从本地选择文件
chooseFile() {
uni.chooseMessageFile({
count: 6, //最多可以选择的文件数量,默认100
extension: ['.pdf'], //所选的文件的类型,默认全部
success: (res) => {
this.fileList = res.tempFiles;
console.log(this.fileList ,'this.fileList ');
// 调用上传文件方法
this.uploadFile()
},
fail: (err) => {
console.log(err);
}
});
},
uploadFile(){
let info = uni.getStorageSync('userInfo')
let that = this
this.fileList.forEach(file=>{
uni.uploadFile({
filePath: file.path,
header: {
'content-type': 'multipart/form-data',
'token': info ? JSON.parse(info).token : ''
},
name: 'file',
url: that.$cfg.httpUrl + 'index/upload',
success: function(res) {
let data = JSON.parse(res.data)
if (data.code == 1) {
that.file.push(data.data.url)
}
},
fail: function(res) {
console.log(res);
}
})
})
},