/**
* 下载文件
*/
download(item) {
const transfer: FileTransferObject = this.transfer.create();
const url = `${FILE_SERVE_URL}jxm/a/attach/attach/anon/download?id=${item.id}`;
var fileName= item.attachName;
// 文件类型
transfer.download(url, this.file.dataDirectory + fileName).then((entry) => {
this.openFile(decodeURI(entry.toURL()))
}, (error) => {
// handle error
alert(error.code);
});
}
/**
* 打开文件
* @param path
*/
openFile(path:string){
this.fileOpener.open(path, this.getFileMimeType(path.substring(path.lastIndexOf(".")+1,path.length)))
.then(() => alert('File is opened'))
.catch(e => alert('Error opening file'+JSON.stringify(e)));
}
/**
* 获取文件类型
* @param fileType
*/
getFileMimeType(fileType: string): string {
let mimeType: string = '';
switch (fileType) {
case 'txt':
mimeType = 'text/plain';
break;
case 'docx':
mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'doc':
mimeType = 'application/msword';
break;
case 'pptx':
mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
break;
case 'ppt':
mimeType = 'application/vnd.ms-powerpoint';
break;
case 'xlsx':
mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'xls':
mimeType = 'application/vnd.ms-excel';
break;
case 'zip':
mimeType = 'application/x-zip-compressed';
break;
case 'rar':
mimeType = 'application/octet-stream';
break;
case 'pdf':
mimeType = 'application/pdf';
break;
case 'jpg':
mimeType = 'image/jpeg';
break;
case 'png':
mimeType = 'image/png';
break;
default:
mimeType = 'application/' + fileType;
break;
}
return mimeType;
}