Windows Server 直接部署 OnlyOffice Document Server(极简版)实现 IIS 网站在线浏览器编辑 Office 文档。
方案特点
无需手动安装依赖:使用官方安装包自动集成 PostgreSQL、RabbitMQ、Erlang
一键式安装:全程仅需运行安装程序,无需配置复杂参数
最小化资源占用:仅保留核心编辑功能,关闭非必要服务
一、安装 OnlyOffice Document Server
1. 下载安装包
2. 运行安装程序(管理员身份)
.\onlyoffice-documentserver.exe /DS_PORT=8080 /S
3. 验证安装
net start | findstr "DsExampleSvc"
二、C# 集成代码(ASP.NET Core 完整示例)
1. 后端代码(DocumentController.cs
)
public class DocumentController : Controller
{
private readonly string _docServerUrl = "http://your-server-ip:端口/"; // 替换为实际地址
private readonly string _storagePath = Path.Combine("App_Data", "Documents");
// 文档编辑页面
[HttpGet]
public IActionResult Edit(string fileName)
{
var config = new
{
documentServerUrl = _docServerUrl,
key = Guid.NewGuid().ToString(),
title = fileName,
url = Url.Content($"~/Documents/{fileName}"),
callbackUrl = Url.Action("Save", "Document", null, Request.Scheme)
};
return View(config);
}
// 保存文档回调接口
[HttpPost]
public IActionResult Save()
{
try
{
var file = Request.Form.Files[0];
var savePath = Path.Combine(_storagePath, file.FileName);
Directory.CreateDirectory(_storagePath); // 确保目录存在
using (var stream = new FileStream(savePath, FileMode.Create))
{
file.CopyTo(stream);
}
return Json(new { error = 0 });
}
catch (Exception ex)
{
return Json(new { error = 1, message = ex.Message });
}
}
}
2. 前端页面(Edit.cshtml
)
@model dynamic
<div id="editor" style="height: 95vh;"></div>
@section Scripts {
<script src="@Model.documentServerUrl/web-apps/apps/api/documents/api.js"></script>
<script>
const config = {
document: {
fileType: "@Model.title.split('.').pop()",
key: "@Model.key",
title: "@Model.title",
url: "@Model.url"
},
editorConfig: {
callbackUrl: "@Model.callbackUrl",
lang: "zh-CN",
user: { id: "user-001", name: "Guest" } // 可自定义用户信息
},
documentServerUrl: "@Model.documentServerUrl"
};
new DocsAPI.DocEditor("editor", config);
</script>
}
三、关键配置说明
1. IIS 配置
.docx → application/vnd.openxmlformats-officedocument.wordprocessingml.document
.xlsx → application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.pptx → application/vnd.openxmlformats-officedocument.presentationml.presentation
2. OnlyOffice 配置优化
"token": { "enable": { "request": { "inbox": false, "outbox": false }, "browser": false } }
"request-filtering-agent": { "allowPrivateIPAddress": true }
3. 数据库简化(可选)
四、系统架构图
浏览器 → IIS ASP.NET Core 应用 → OnlyOffice Document Server (8080)
↑ ↑
SQL Server 数据库 内嵌 PostgreSQL/RabbitMQ
↑
文件存储(App_Data/Documents)
五、常见问题与排查
问题现象 | 解决方法 |
---|
文档无法加载 | 检查 OnlyOffice 服务是否运行,防火墙是否开放端口 |
保存回调失败 | 确保 callbackUrl 可通过公网访问,关闭 JWT 验证 |
中文文件名乱码 | 前端传递文件名时使用 encodeURIComponent() ,后端解码保存 |
内存占用过高 | 建议服务器内存 ≥4GB,或限制并发编辑用户数 |
端口冲突 | 修改 OnlyOffice 端口(如 8080),避免与 IIS 冲突 |
六、扩展功能(可选)
用户权限控制:在 editorConfig
中设置 permissions
字段限制编辑权限
版本历史:结合 SQL Server 记录每次保存的版本
文档预览:通过 /ConvertService.ashx
接口生成 PDF 预览
通过此方案,您可在 30 分钟内完成 OnlyOffice 的部署与集成,实现基础的在线编辑功能。如需进一步优化,可参考 ONLYOFFICE API 文档。
该文章在 2025/5/28 11:28:19 编辑过