"docx-preview": "^0.1.16",
const exportFile = async (item: any) => {
if (state.levelObj.explevel !== "1") {
return;
}
if (item.file_type === "docx") {
window.open(
"/#/preview" + "?file=" + item.file_id + "&name=" + item.file_name
);
} else {
downloadFileById({ id: item.file_id }).then((res) => {
downloadFile(res.data, item.file_name, item.file_type, item.file_id);
});
}
};
const exportFileAll = async (row: any) => {
let url =
window.location.origin +
`/techAssess/sysFile/downloadFileByBusinessId?businessId=${row.id}`;
getExportFile(url);
};
export function downloadFile(
fileStream: string,
fileNames: string,
fileFormat: string,
fileId: string
): void {
let applicationType = { type: 'application/vnd.ms-excel' };
if (fileFormat === 'pdf' || fileFormat === 'doc') {
applicationType = { type: 'application/pdf' };
}
const blob = new Blob([fileStream], applicationType);
if ('download' in document.createElement('a')) {
const elink = document.createElement('a');
elink.setAttribute('download', fileNames + '.' + fileFormat);
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
if (fileFormat === 'pdf') {
window.open('/#/preview' + '?url=' + URL.createObjectURL(blob));
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
} else if (fileFormat === 'doc') {
previewDocAndPdf(fileId).then(res => {
if (res.data.data) {
window.open(`${process.env.VUE_APP_FILE_URL}/${res.data.data}`);
}
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
});
} else {
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
}
} else {
}
}
export const getExportFile = (url: string) => {
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", "文件名");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
export function downloadFileById(params: { id: string }): AxiosPromise<any> {
return request({
url: 'sysFile/downloadFileById',
method: 'GET',
responseType: 'blob',
params
});
}
export function previewDocAndPdf(id: string): AxiosPromise<SuccessInfo<any>> {
return request({
url: `sysFile/previewDocAndPdf?fileId=${id}`,
method: 'GET'
});
}