ONLYOFFICE Docs 是一个文档中间件,为文档管理软件提供 Office 格式的文档的在线预览与编辑。
支持主流格式:docx、xlsx、pptx、odt、ods、odp、doc、xls、ppt、pdf、txt、rtf、html、epub、csv。
Docker部署OnlyOffice
docker run -i -t -d -p 45680:80 --restart=always -e JWT_ENABLED=false onlyoffice/documentserver
预览完整源码
<html lang="zh-CN">
<head>
<title>Demo</title>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta charset="utf-8" />
<!-- onlyoffice服务器地址 -->
<script src="http://192.168.1.131:40156/web-apps/apps/api/documents/api.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id="placeholder"></div>
<script>
function preview(url, filename) {
const index = filename.lastIndexOf('.');
const fileType = filename.substr(index + 1);
const config = {
"document": {
"permissions": {
comment: false,
fillForms: false,
"edit": false,
},
"fileType": fileType,
"title": filename,
"url": url,
"lang": "zh-CN"
},
"width": '100%',
"editorConfig": {
mode: 'view',
"lang": "zh-CN"
}
};
document.title = filename;
const docEditor = new DocsAPI.DocEditor("placeholder", config);
}
const currentUrl = window.location.href;
const url = new URL(currentUrl);
const params = new URLSearchParams(url.search);
const fileUrl = params.get('url');
const filename = params.get('filename');
const validExtensions = ['docx', 'xlsx', 'pptx', 'odt', 'ods', 'odp', 'doc', 'xls', 'ppt', 'pdf', 'txt', 'rtf', 'html', 'epub', 'csv'];
const getFileExtension = (filename) => {
return filename ? filename.split('.').pop().toLowerCase() : '';
};
console.log('文件url:' + fileUrl);
console.log('文件name:' + filename);
if (fileUrl && filename) {
const fileExtension = getFileExtension(filename);
if (validExtensions.includes(fileExtension)) {
preview(fileUrl, filename);
} else {
alert(`错误: 文件格式不受支持。支持的格式有: ${validExtensions.join(', ')}`);
}
} else {
let missingParams = [];
if (!fileUrl) missingParams.push('url');
if (!filename) missingParams.push('filename');
alert(`缺少参数: ${missingParams.join(', ')}`);
}
</script>
</body>
</html>
调用方式 url传参
- url - 文件地址
- filename - 文件名
http://ip:port/onlyoffice_view.html?url=http://ip:port/file&filename=filename.docx
此处评论已关闭