【web】 预览现在功能实现

dev
Wangxin 8 months ago
parent f095eb13ad
commit d036f6de7f
  1. 267
      ALOps_sys_fe/alops-ui/src/views/QandA/knowledge/index.vue

@ -27,8 +27,10 @@
</el-table-column>
<el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
<template slot-scope="scope" v-if="scope.row.id !== 1">
<el-button size="mini" type="text" icon="el-icon-view" @click="previewFile(scope.row)" v-hasPermi="['QandA:knowledge:preview']">预览</el-button>
<el-button size="mini" type="text" icon="el-icon-download" @click="downloadFile(scope.row)" v-hasPermi="['QandA:knowledge:filedownload']">下载</el-button>
<el-button size="mini" type="text" icon="el-icon-view" @click="previewFile(scope.row)" v-hasPermi="['QandA:knowledge:filepreview']">预览</el-button>
<a href="javascript:void(0)" class="el-button el-button--text el-button--mini" @click="downloadFile(scope.row)" v-hasPermi="['QandA:knowledge:filedownload']">
<i class="el-icon el-icon-download"></i> 下载
</a>
<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['QandA:knowledge:update']">修改</el-button>
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['QandA:knowledge:del']">删除</el-button>
</template>
@ -82,7 +84,7 @@
</template>
<script>
import { list, del, add, update, permanentKnowledgeList, uploadedFilesList, downloadFile } from "@/api/QandA/knowledge"
import { list, del, add, update, permanentKnowledgeList, uploadedFilesList } from "@/api/QandA/knowledge"
import { getToken } from "@/utils/auth"
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"
@ -381,61 +383,228 @@ export default {
submitFileForm() {
this.$refs.upload.submit()
},
//
// -
previewFile(row) {
//
const filePath = this.appBase + row.filePath;
// token
const token = this.$store.getters.token;
//
// URL使
const previewUrl = `${this.appBase}/QandA/knowledge/file/download/${row.id}`;
// XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.open('GET', previewUrl, true);
//
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
//
if (row.format === 'txt') {
// txt
window.open(filePath, '_blank');
} else if (row.format === 'docx') {
// docx使线
// 使
window.open(filePath, '_blank');
// 使线
// const previewUrl = `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(filePath)}`;
// window.open(previewUrl, '_blank');
xhr.responseType = 'text'; //
} else if (['png', 'jpg', 'jpeg', 'gif', 'bmp'].includes(row.format.toLowerCase())) {
xhr.responseType = 'blob'; // Blob
} else if (row.format === 'pdf') {
xhr.responseType = 'blob'; // PDFBlob
} else {
//
this.$message.info('暂不支持此文件格式的预览');
//
xhr.responseType = 'text';
}
//
xhr.onload = function() {
if (xhr.status === 200) {
if (row.format === 'txt') {
//
this.previewTextFile(xhr.response, row.source);
} else if (['png', 'jpg', 'jpeg', 'gif', 'bmp'].includes(row.format.toLowerCase())) {
//
this.previewImageFile(xhr.response, row.source);
} else if (row.format === 'pdf') {
// PDF
this.previewPdfFile(xhr.response, row.source);
} else if (row.format === 'docx') {
// Word - 使线
const docxUrl = URL.createObjectURL(new Blob([xhr.response]));
const onlinePreviewUrl = `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(docxUrl)}`;
window.open(onlinePreviewUrl, '_blank');
} else {
//
try {
const textContent = xhr.response;
if (textContent && textContent.length > 0) {
this.previewTextFile(textContent, row.source);
} else {
this.$message.info('暂不支持此文件格式的预览');
}
} catch (e) {
this.$message.info('暂不支持此文件格式的预览');
}
}
} else {
this.$message.error('文件预览失败,请重试');
}
}.bind(this);
//
xhr.onerror = function() {
this.$message.error('网络错误,文件预览失败');
}.bind(this);
//
xhr.send();
},
//
previewTextFile(content, fileName) {
//
const previewWindow = window.open('', '_blank', 'width=800,height=600');
previewWindow.document.write(`
<html>
<head>
<title>${fileName} - 预览</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; margin: 0; background-color: #f5f5f5; }
.container { max-width: 100%; margin: 0 auto; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
pre { white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New', monospace; font-size: 14px; line-height: 1.5; }
h1 { margin-top: 0; color: #333; font-size: 20px; }
</style>
</head>
<body>
<div class="container">
<h1>${fileName}</h1>
<pre>${content}</pre>
</div>
</body>
</html>
`);
previewWindow.document.close();
},
//
previewImageFile(blob, fileName) {
// URL
const imageUrl = URL.createObjectURL(blob);
//
const previewWindow = window.open('', '_blank', 'width=800,height=600');
previewWindow.document.write(`
<html>
<head>
<title>${fileName} - 预览</title>
<style>
body { margin: 0; padding: 20px; background-color: #f5f5f5; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.container { max-width: 100%; max-height: 100%; text-align: center; }
img { max-width: 100%; max-height: 80vh; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h1 { color: #333; font-size: 20px; margin-bottom: 20px; }
</style>
</head>
<body>
<div class="container">
<h1>${fileName}</h1>
<img src="${imageUrl}" alt="${fileName}">
</div>
</body>
</html>
`);
previewWindow.document.close();
// URL
previewWindow.onunload = function() {
URL.revokeObjectURL(imageUrl);
};
},
// PDF
previewPdfFile(blob, fileName) {
// PDF URL
const pdfUrl = URL.createObjectURL(blob);
//
const previewWindow = window.open('', '_blank', 'width=800,height=600');
previewWindow.document.write(`
<html>
<head>
<title>${fileName} - 预览</title>
<style>
body { margin: 0; padding: 0; height: 100vh; overflow: hidden; }
iframe { width: 100%; height: 100%; border: none; }
</style>
</head>
<body>
<iframe src="${pdfUrl}"></iframe>
</body>
</html>
`);
previewWindow.document.close();
// URL
previewWindow.onunload = function() {
URL.revokeObjectURL(pdfUrl);
};
},
//
// - 使XMLHttpRequest
downloadFile(row) {
// 使API
downloadFile(row.id).then(response => {
// 使row.source
let fileName = row.source;
const contentDisposition = response.headers['content-disposition'];
if (contentDisposition && contentDisposition.indexOf('filename=') !== -1) {
fileName = decodeURIComponent(contentDisposition.split('filename=')[1]);
// token
const token = this.$store.getters.token;
// URL
const downloadUrl = `${this.appBase}/QandA/knowledge/file/download/${row.id}`;
// XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.open('GET', downloadUrl, true);
//
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
// blob
xhr.responseType = 'blob';
//
xhr.onload = function() {
if (xhr.status === 200) {
//
let fileName = row.source;
const contentDisposition = xhr.getResponseHeader('content-disposition');
if (contentDisposition && contentDisposition.indexOf('filename=') !== -1) {
fileName = decodeURIComponent(contentDisposition.split('filename=')[1]);
}
// Blob
const blob = new Blob([xhr.response]);
//
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.style.display = 'none';
// DOM
document.body.appendChild(link);
link.click();
//
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
//
this.$message.success('文件下载成功');
} else {
//
this.$message.error('文件下载失败,请重试');
}
}.bind(this);
//
xhr.onerror = function() {
this.$message.error('网络错误,文件下载失败');
}.bind(this);
//
xhr.send();
// Blob
const blob = new Blob([response.data]);
//
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.style.display = 'none';
// DOM
document.body.appendChild(link);
link.click();
//
document.body.removeChild(link);
URL.revokeObjectURL(link.href); // URL
//
this.$message.success('文件下载成功');
}).catch(error => {
console.error('文件下载失败:', error);
this.$message.error('文件下载失败,请重试');
});
//
this.$message.success('正在下载文件...');
}
}
}

Loading…
Cancel
Save