LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#.net / Web开发】Windows Server 直接部署 OnlyOffice Document Server(极简版)实现IIS网站浏览器在线编辑Office文档

admin
2025年5月28日 11:24 本文热度 820
Windows Server 直接部署 OnlyOffice Document Server(极简版)实现 IIS 网站在线浏览器编辑 Office 文档。

方案特点

  • 无需手动安装依赖:使用官方安装包自动集成 PostgreSQL、RabbitMQ、Erlang

  • ​一键式安装:全程仅需运行安装程序,无需配置复杂参数

  • 最小化资源占用:仅保留核心编辑功能,关闭非必要服务

一、安装 OnlyOffice Document Server

1. 下载安装包

2. 运行安装程序(管理员身份)

  • 安装路径:建议 C:\ONLYOFFICE(避免中文或空格)

  • 端口设置

    • 若 IIS 已占用 80 端口,改为其他端口(如 8080

    • 命令行静默安装示例:(powershell)

    .\onlyoffice-documentserver.exe /DS_PORT=8080 /S

3. 验证安装

  • 访问 http://服务器IP:端口/welcome/,出现欢迎页即成功

  • 检查服务是否运行(服务名 DsExampleSvc):powershell

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 配置

  • 应用程序池:设置为 无托管代码

  • MIME 类型

.docx → application/vnd.openxmlformats-officedocument.wordprocessingml.document  
.xlsx → application/vnd.openxmlformats-officedocument.spreadsheetml.sheet  
.pptx → application/vnd.openxmlformats-officedocument.presentationml.presentation
  • 目录权限:确保 App_Data/Documents 目录有写入权限

2. OnlyOffice 配置优化

  • 关闭 JWT 验证(简化调试):
    修改 C:\ONLYOFFICE\DocumentServer\config\local.json

"token": { "enable": { "request": { "inbox": false, "outbox": false }, "browser": false } }
  • 允许私有 IP 访问
    修改 C:\ONLYOFFICE\DocumentServer\config\default.json

"request-filtering-agent": { "allowPrivateIPAddress": true }

3. 数据库简化(可选)

  • 默认使用安装包内嵌的 PostgreSQL,无需额外操作

  • 若需替换为 SQL Server:
    修改 C:\ONLYOFFICE\DocumentServer\config\local.json 中的数据库配置段(需手动迁移数据)

四、系统架构图

浏览器 → IIS ASP.NET Core 应用 → OnlyOffice Document Server (8080)
               ↑                           ↑
        SQL Server 数据库        内嵌 PostgreSQL/RabbitMQ
               ↑
        文件存储(App_Data/Documents)

五、常见问题与排查

问题现象解决方法
文档无法加载检查 OnlyOffice 服务是否运行,防火墙是否开放端口
保存回调失败确保 callbackUrl 可通过公网访问,关闭 JWT 验证
中文文件名乱码前端传递文件名时使用 encodeURIComponent(),后端解码保存
内存占用过高建议服务器内存 ≥4GB,或限制并发编辑用户数
端口冲突修改 OnlyOffice 端口(如 8080),避免与 IIS 冲突

六、扩展功能(可选)

  1. 用户权限控制:在 editorConfig 中设置 permissions 字段限制编辑权限

  2. 版本历史:结合 SQL Server 记录每次保存的版本

  3. 文档预览:通过 /ConvertService.ashx 接口生成 PDF 预览

通过此方案,您可在 30 分钟内完成 OnlyOffice 的部署与集成,实现基础的在线编辑功能。如需进一步优化,可参考 ONLYOFFICE API 文档


该文章在 2025/5/28 11:28:19 编辑过
关键字查询
相关文章
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved