当前位置: 首页 > news >正文

Vue 2.0 + C# + OnlyOffice 开发

开发环境:

Windows系统、Docker运行环境、Vue2.0、.net core

安装OnlyOffice:

1. 拉取镜像:

docker pull onlyoffice/documentserver:latest

2. 启动容器:JWT_ENABLED=false 环境下需要关闭JWT校验。

docker run -d -p 9000:80 -v /path/to/data:/var/www/onlyoffice/Data -v /path/to/logs:/var/log/onlyoffice --restart=always -e JWT_ENABLED=false onlyoffice/documentserver:latest

 访问:http://127.0.0.1:9000/welcome/

呈现以下界面:

3. 修改配置文件:文件允许私有ip通过,不然Docker中的镜像服务无法访问到文件服务器。

  • 访问容器:

docker exec -it <镜像ID> bash如:
docker exec -it 498a72efb326f8cff5449799dd2992a5f35dd92a5357d903d1fd0491e692e784 bash
  • 安装 VIM:

apt-get update && apt-get install -y vim
  • 编译default.json文件:
vim /etc/onlyoffice/documentserver/default.json
将:allowPrivateIPAddress和allowMetaIPAddress设置成True
"request-filtering-agent" : {"allowPrivateIPAddress": true,"allowMetaIPAddress": true
},

4. 测试网络环境:

  • 检查容器网络:sysctl net.ipv4.ip_forward # 输出应为 net.ipv4.ip_forward = 1

  • 测试容器镜像是否能够Ping通文件服务器。

Vue 2.0 编写代码:

1. 创建components组件:OnlyOfficeEditor.vue


<template><div class="onlyoffice-container"><div v-if="loading" class="loading">编辑器加载中...</div><div v-if="error" class="error">编辑器加载失败: {{ error }}</div><div v-show="!loading && !error" :id="editorId" style="height: 800px"></div></div>
</template><script>
export default {props: {config: {type: Object,required: true,validator: (config) => {return ['url', 'title'].every((key) => key in config)},},},data() {return {editorId: `onlyoffice-${Math.random().toString(36).substr(2, 9)}`,loading: true,error: null,docEditor: null,}},mounted() {this.initEditor()},methods: {async initEditor() {try {await this.loadScript()this.setupEditor()} catch (err) {this.error = err.message} finally {this.loading = false}},loadScript() {return new Promise((resolve, reject) => {if (window.DocsAPI) return resolve()const script = document.createElement('script')script.src =this.config.apiUrl ||'http://127.0.0.1:9000/web-apps/apps/api/documents/api.js'script.onload = () => {let attempts = 0const checkAPI = () => {attempts++if (window.DocsAPI) {resolve()} else if (attempts < 5) {setTimeout(checkAPI, 300)} else {reject(new Error('OnlyOffice API加载超时'))}}checkAPI()}script.onerror = () => reject(new Error('OnlyOffice API脚本加载失败'))document.head.appendChild(script)})},setupEditor() {const defaultConfig = {document: {fileType: 'xlsx',key: `xlsx-${Date.now()}`,title: this.config.title,url: this.config.url,permissions: {edit: this.config.editable !== false,download: true,print: true,},},editorConfig: {callbackUrl: this.config.callbackUrl,lang: 'zh-CN',user: {id: this.config.userId || 'anonymous',name: this.config.userName || '匿名用户',},customization: {autosave: true,forcesave: false,},},}this.docEditor = new window.DocsAPI.DocEditor(this.editorId, {...defaultConfig,...this.config.extraOptions,})},},beforeDestroy() {if (this.docEditor) {this.docEditor.destroyEditor()}},
}
</script><style scoped>
.onlyoffice-container {position: relative;height: 100%;
}
.loading,
.error {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);
}
.error {color: red;
}
</style>

2. 创建主界面:text.vue

<template><div style="width: 100%; height: 1024px;"><h1>运维手册</h1><only-office-editor style="width: 100%; height: 100%;" :config="editorConfig" @loaded="onEditorLoaded" @error="onEditorError" /></div>
</template><script>
import OnlyOfficeEditor from '@/components/OnlyOfficeEditor.vue'export default {components: { OnlyOfficeEditor },data() {return {editorConfig: {title: '测试记录1.0.2.xlsx',url: 'http://192.168.1.210:10177/测试记录1.0.2.xlsx',callbackUrl: 'http://localhost:2380/SaveCallback',userId: 'vue-user',userName: 'Vue操作员',editable: true,extraOptions: {height: '1000vh'}}}},methods: {onEditorLoaded() {console.log('编辑器加载完成')},onEditorError(err) {console.error('编辑器错误:', err)}}
}
</script>

创建后台回调接口

1. 当前回调API接口使用.net core 3.0 开发

/// <summary>
/// center for the API
/// </summary>
public class TestDemoController : ApiController
{[HttpGet, Route("GetDocument")]public IHttpActionResult GetDocument(string docKey){var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Documents", docKey);return Ok(new{url = $"http://127.0.0.1:9000/documents/{docKey}",fileType = Path.GetExtension(docKey).TrimStart('.')});}[HttpPost, Route("SaveCallback")]public IHttpActionResult SaveCallback([FromBody] CallbackModel model){if (model.status == 2) // 2表示文档已保存{var savePath = Path.Combine("C:/SavedDocs", model.key);new WebClient().DownloadFile(model.url, savePath);}return Json(new { error = 0 });}public class CallbackModel{public int status { get; set; }public string key { get; set; }public string url { get; set; }}}

最终呈现:

相关文章:

  • GO自带日志库log包解释
  • RAG->大模型搜索search-R1
  • Java中高并发线程池的相关面试题详解
  • AE之番外篇
  • 模型上下文协议(MCP)实践指南
  • 深入解析默认值工具类:DefaultUtil
  • TF-IDF算法详解与实践总结
  • 上海市计算机学会竞赛平台第六届上海市青少年算法竞赛网络赛(青年组)平方的和
  • Vue2 第一节_Vue2上手_插值表达式{{}}_访问数据和修改数据_Vue开发者工具
  • 【2025CVPR】花粉识别新标杆:HieraEdgeNet多尺度边缘增强框架详解
  • 【PhysUnits】17.6 Unit基础结构(unit.rs)
  • python模拟键盘 鼠标操作 通过ctypes调用Windows API实现底层输入模拟
  • Android Studio 问题:Android Studio 一直开在 Updating indexes
  • 使用Mvnd加速Maven构建速度
  • 深度学习核心概念:优化器、模型可解释性与欠拟合
  • TI以太网PHY收发器晶体选择和规格
  • OpenGL学习20250610
  • 定时器任务——若依源码分析
  • 376. Wiggle Subsequence
  • Windows cmd中文乱码解决方法(Windows控制台中文乱码、CMD乱码、控制台乱码、Command Prompt命令提示符cmd.exe乱码)
  • 腾讯云学生怎么做网站的/百度旗下的所有产品
  • 网站优化http://www.seo668.cn/seo/橙子建站
  • 国外建站主机/在哪个平台做推广比较好
  • 网站优化需要什么软件/网络营销有哪些内容
  • 网站怎么更改布局/宁波seo关键词费用
  • 怎么网站建设公司/网站链接分析工具