.Net Core集成Office Web Apps(二)
想要使用OWA需要一台单独的服务器来部署,这对很多人造成困难。而写该文的目的是为了分享有个OWA的集成步骤,它不仅适用于.Net开发环境,其它语言也是一样的,只要实现了需要的服务接口。并且该文不局限与OWA的研究,还包括Config、XML、Cache、Redis等技术。更重要的是熟悉.Net Core开发。
在OwaFileInfo的SupportsUpdate、UserCanWrite、SupportsLocks属性为true时将允许用户在线编辑文件。
在原有的基础上编辑一个test.xlsx文件,其访问链接为(测试链接没有实际意义):http://owa.test.com/x/_layouts/xlviewerinternal.aspx?edit=1&WOPISrc=http%3a%2f%2f192.168.1.1%2fapi%2fwopi%2ffiles%2ftest.xlsx&access_token=H7lFBYT4pVMK
将弹出如下对话框
点击编辑副本
查看请求日志发现,另存副本请求将调用PutRelativeFile服务,所有我们应该再原来的基础上添加PutRelativeFile服务。
PutRelativeFile服务
PutRelativeFile操作将在主机上创建一个基于当前文件的副本文件,创建成功后必须将副本文件名及URL以Json的格式返回给OWA服务器。
如果OWA客户端设置了CheckFileInfo的SupportsUpdate为true的话,就必须要实现PutRelativeFile服务,否则需要设置UserCanNotWriteRelative为true并且返回501状态码。
Method:POST
URI:HTTP://server/<...>/wopi*/files/<id>
Request Headers:
X-WOPI-Override 固定值 PUT_RELATIVE
X-WOPI-SuggestedTarget 文件扩展名或全名(可修改)
X-WOPI-RelativeTarget 文件全名(不可更改)
X-WOPI-OverwriteRelativeTarget 是否覆盖文件名
X-WOPI-Size 文件的大小bytes
X-WOPI-FileConversion 指示请求是否转换文件
Response Headers:
X-WOPI-ValidRelativeTarget
X-WOPI-Lock
X-WOPI-LockFailureReason
该接口需要返回的Json格式如下:
{Name:”test.xlsx”,Url:” http://server/<...>/wopi/files/ test.xlsx”, HostViewUrl :””, HostEditUrl :””}
说明:Name为新的文件名(如果修改的话),Url为新的文件的CheckFileInfo服务路径。
在线Excel用到这个服务的情况有两种:
1.另存为,如果没有实现PutRelativeFile服务,则另存为将不能使用;
2.当编辑服务器不支持的Excel某些格式的文件时,会先保存一个系统支持格式的副本,如果没有实现PutRelativeFile服务,则不能编辑该Excel(因为保存新文件会失败);
PutRelativeFile服务与CheckFileInfo服务的URL是相同的,只是Method不同,所以我们只需在CheckFileInfo的基础上修改。
[RouteAttribute("files/{name}")]
public JsonResult GetFileInfo(string name, string access_token)
{
string wopiType = Request.Headers["X-WOPI-Override"];
Console.WriteLine("1.X-WOPI-Override:" + wopiType);
if (wopiType == "PUT_RELATIVE")
{
using (FileStream fs = System.IO.File.Create("Files/new_" + name))
{
Request.Body.CopyTo(fs);
}
Response.Headers.Add("X-WOPI-ValidRelativeTarget", "new_" + name);
Response.Headers.Add("X-WOPI-Lock", Request.Headers["X-WOPI-Lock"]);
Response.StatusCode = ;
PutRelativeFile file = new PutRelativeFile();
file.Name = "new_" + name;
file.Url = "http://b1wcfoqm7r.proxy.qqbrowser.cc/api/wopi/files/new_" + name;
return Json(file);
}
else if (wopiType == "UNLOCK" || wopiType == "LOCK")
{
Response.Headers.Add("X-WOPI-Lock", Request.Headers["X-WOPI-Lock"]);
Response.StatusCode = ;
return Json("");
}
else
{
FileHelper helper = new FileHelper();
var info = helper.GetFileInfo(name);
info.SupportsUpdate = true;
info.SupportsLocks = true;
info.UserCanWrite = true;
return Json(info);
}
GetFileInfo
[DataContract(Name = "PutRelativeFile")]
public class PutRelativeFile
{
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Url")]
public string Url { get; set; }
[DataMember(Name = "HostViewUrl")]
public string HostViewUrl { get; set; }
[DataMember(Name = "HostEditUrl")]
public string HostEditUrl { get; set; }
}
PutRelativeFile
Discovery.xml
访问OWA服务器的如下地址:http://owa.host.com/hosting/discovery(该链接无实际意义)就能得到OWA服务器能够处理的所有文件格式、操作及相关的URL。
<action name="view" ext="xls" default="true" urlsrc="http://owa.hand-china.com/x/_layouts/xlviewerinternal.aspx?<ui=UI_LLCC&><rs=DC_LLCC&>"/>
name=”view”表示动作为查看,ext=”xls”表示文件扩展名为xls,urlsrc则表示相应动作的地址。
以预览test.xls为例,其ChecFileInfo路径为:http://localhost:5000/api/wopi/files/test.xls
则最终预览的链接为:http://owa.host.com/x/_layouts/xlviewerinternal.aspx?WOPISrc=http://localhost:5000/api/wopi/files/test.xls
我将OWA服务提交的discovery内容放到了本地的xml文件中,读取方法如下:
public static List<DiscoveryAction> GetDiscoveryAction()
{
List<DiscoveryAction> list = new List<DiscoveryAction>();
XmlReader reader = XmlReader.Create("Files/Discovery.xml");
//循环Read方法直到文档结束
while (reader.Read())
{
//Console.WriteLine("rdr.NodeType = " + reader.NodeType);
//如果是开始节点
if (reader.NodeType == XmlNodeType.Element)
{
//通过rdr.Name得到节点名
string elementName = reader.Name; //读取到cat元素 这时rdr.Read()读取到的内容为<cat color="white">
if (elementName == "action")
{
DiscoveryAction act = new DiscoveryAction();
//可以通过中括号获得属性值
act.Name = reader["name"];
act.UrlSrc = reader["urlsrc"];
act.Default = TransBool(reader["default"]);
act.Ext = reader["ext"];
act.ProGid = reader["progid"];
act.Requires = reader["requires"];
list.Add(act);
}
}
}
return list;
}
GetDiscovery
Discovery.xml文件下载:Disconvery
Controller Action代码如下:
[Route("files/{name}/links")]
[HttpGetAttribute]
public JsonResult GetOwaLinks(string name, string type, string access_token, string lang)
{
if (string.IsNullOrEmpty(type))
{
type = "view";
}
if (string.IsNullOrEmpty(lang))
{
lang = "zh-CN";
}
if (string.IsNullOrEmpty(access_token))
{
access_token = "H7lFBYT4pVMK";
}
List<string> lst = new List<string>();
var res = XmlHelper.GetDiscoveryAction();
var ext = Path.GetExtension(name).TrimStart('.');
var discovery = res.Where(t => t.Ext == ext && t.Name == type);
foreach (var item in discovery)
{
Console.WriteLine(item.UrlSrc);
var index = item.UrlSrc.IndexOf('<');
var api = System.Net.WebUtility.UrlEncode("http://localhost:5000/api/wopi/files/" + name);
lst.Add(string.Format(item.UrlSrc.Substring(, index) + "WOPISrc={0}&access_token={1}&ui={2}", api, access_token, lang));
}
return Json(lst);
}
GetOwaLinks
测试访问:http://localhost:5000/api/wopi/files/test.docx/links
.Net Core集成Office Web Apps(二)的更多相关文章
- .Net Core集成Office Web Apps(一)
最近开始学习.Net Core,并使用Visual Studio Code工具来开发.感觉开发起来特别的方便,但是有个头疼的地方:许多的类库被修改了,一时半会儿还熟悉不了,需要查阅官方API... M ...
- SharePoint RBS 安装(集成Office Web Apps)
前言 本文完全原创,转载请说明出处,希望对大家有用. 本篇博客是个人总结,一方面以便日后查看,另一方面希望能为其他人提供一些便利. 阅读目录 安装RBS 为多个内容数据库开启RBS 正文 目的:在Sh ...
- 部署Office Web Apps Server并配置其与SharePoint 2013的集成
部署Office Web Apps Server并配置其与SharePoint 2013的集成 Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...
- 新手必看!Office Web Apps 2013 安装与配置(实战)
分享人:广州华软 星尘 一. 前言 Office Web Apps Server 是Office 服务器产品,它可提供在Sharepoint 2013网站中在线浏览和编辑 Word.PowerPoin ...
- 如何整合Office Web Apps至自己开发的系统(一)
在前面我的一篇博客中 Office Web Apps安装部署(一),有一张介绍Office Web Apps与其他系统的关系图, 从上述图中,可知实际上Office Web Apps也是可以接入自 ...
- 整合Office Web Apps至自己的开发系统
原文出处:http://www.cnblogs.com/poissonnotes/p/3267190.html 还可参考:https://www.cnblogs.com/majiang/p/36729 ...
- [转载]部署Office Web Apps Server并配置其与SharePoint 2013的集成
Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.PowerPoint.Excel 和 OneNote 的基于浏览器的版本.单个 Office Web ...
- 微软office web apps 服务器搭建之在线文档预览(二)
上一篇文章已经介绍了整个安装过程了.只要在浏览器中输入文档转换server的ip,会自动跳转,出现如下页面. 那么就可以实现本地文档预览了,你可以试试.(注意:是本地哦,路径不要写错,类似“\\fil ...
- sharepoint 2013 和 office web apps server 2013集成
环境: 三台服务器 系统:window 2008 R2server01: 192.168.10.162(office web app)server02: 192.168.10.163(AD)serv ...
随机推荐
- Shell初学(四)运算符
一.算术运算符 下表列出了常用的算术运算符,假定变量 a 为 10,变量 b 为 20: 运算符 说明 举例 + 加法 `expr $a + $b` 结果为 30. - 减法 `expr $a - $ ...
- UILabel富文本 段落格式以及UILabel添加图片
之前文本整理有一点乱,这边重新整理一下,下面是效果图,一共两个UILabel, 富文本整理: /*NSForegroundColorAttributeName设置字体颜色,对象UIColor; NSP ...
- latex 转word
1:下载pandoc软件,支持多种文件格式互转. http://www.pandoc.org/installing.html#windows 2:下载zip包,解压,并将含有pandoc.exe的目录 ...
- MVC中Controller与View之间的数据传递
一.Controller向View传递数据 Controller向View传递数据有3种形式: 通过ViewData传递 在Controller里面的Action方法中定义ViewData,并且赋值, ...
- Bind()事件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php hash算法
任意长度的输入, 固定长度的输出 ,该输出就是hash值,这种转换就是一种压缩映射,也就是hash值的空间远远小于输入的空间, 不同的输入可能散列成相同的输出,而不能从hash值来唯一的确定输入值. ...
- chkconfig添加进入服务后,出现的现象
比如在php-fpm添加服务中,一部分脚步如下 #!/bin/sh # # php-fpm - this script starts and stops the php-fpm ...
- MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九
<Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次 SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...
- PKU2503_map应用
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- fontawesome与amazeUI
fontawesome是很不错的东西呢~~~~ amazeUI也是很不错的东西呢~~~~~ 最近一年里,比较偏爱这两个家伙? 什么?为什么bootstrap?有什么区别? 这个我还真说不太清楚. 用一 ...