导出(下载)
// 下载文件资源
var timestamp = new Date().getTime();
uni.downloadFile({
url: 'https://example.com/audio/123', //仅为示例,并非真实的资源
header:{ token:uni.getStorageSync('token'),"Content-Type":'application/vnd.ms-excel'},
filePath: 'wxfile://temp/' + timestamp + '.xls',
success (res) {
// 保存文件资源
wx.saveFileToDisk({
filePath: res.tempFilePath,
filePath: 'wxfile://temp/' + timestamp + '.xls',
success:(ret)=>{
console.log(ret)
that.toast('保存成功');
},
fail:(rey)=>{
console.log(rey)
that.toast('保存失败');
}
})
}
})
导入(上传、选择文件)
// 选择文件资源
var that=this;
uni.chooseMessageFile({
count: 1,
type: 'file',
success(ress) {
let type = ress.tempFiles[0].type
// console.log(type)
if (ress.tempFiles.length > 0 ) {
if (ress.tempFiles[0].size < 8388608) {
uni.showLoading({ title:'上传中' })
let tempFilePaths = ress.tempFiles
// 下载保存文件资源
uni.uploadFile({
url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0].path,
name: 'file',
header:{
"token": uni.getStorageSync('token')||'',
},
formData: { "file": tempFilePaths[0].path },
success: (res) => {
var d=JSON.parse(res.data);
that.toast(d.msg);
if(d.code==1){
uni.hideLoading();
}
}
});
} else {
that.toast('超出限制大小');
return
}
} else {
that.toast('文件格式错误');
}
}
})