流程是先将文件下载,到本地,然后使用openDocument打开本地路径
需要下载WPS文件,以下是模拟器打开效果
使用plus.downloader.createDownload 方法将新建下载任务
export default function plusDownload(config){
if(!config)
{
console.error("Argument should not be null");
return;
}
const url=request.baseUrl+config.url;
let token = uni.getStorageSync("token");
let header={
// 'Content-Type':"application/octet-stream",
'Authorization':"Bearer " + token
//'Accept':'*/*'
};
config.header=config.header||{};
Object.assign(header,config.header);
let download= plus.downloader.createDownload(url,config,(d,s)=>config.complete(d,s,download));
download.setRequestHeader('Authorization',"Bearer " + token);
download.start();
}
封装方法
export function downloadInspectionResult(data) {
uni.showLoading({title:'读取文件...',duration:10000});
plusDownload({
url:'/ny-iqc/incomingInspection/exportCoa?id='+data.id,
method: 'POST',
filename: '_downloads/'+data.fileName,
header: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: data,
complete: function (res,status,download) {
uni.hideLoading();
uni.openDocument({
filePath: res.filename,
fileType: 'xlsx',
showMenu: true,
success: () => {
uni.showToast({
title: '打开文档成功',
icon: 'success',
mask: true,
});
},
fail: (error) => {
console.log('error', error);
},
});
},
});
}
调用封装的方法
downloadInspectionResult({
id:id,
fileName: fileName
});