应用程序与 Office Web Apps 整合

关于部署的细节,可以参考我的另一篇文章《在线浏览 Office 文档之 Office Web Apps》

关于 Wopi 协议,可以参考如下的链接:

[MS-WOPI]:Web Application Open Platform Interface Protocol

https://msdn.microsoft.com/en-us/library/hh622722(v=office.12).aspx

IntroducingWOPI

http://blogs.msdn.com/b/officedevdocs/archive/2013/03/21/introducing-wopi.aspx

Buildingan Office Web Apps (OWA) WOPI Host:

https://code.msdn.microsoft.com/office/Building-an-Office-Web-f98650d6

CSDN 上面也有类似的文章,基本上也就是根据上面的文章翻译过来并加上了自己理解的: 如何整合Office Web Apps至自己开发的系统(一) 。如果你认真读了这些文章,那么你现在应该知道为了将自己的程序(Wopi Host)与Office Web Apps (Wopi Client)整合起来,Wopi Host至少得提供两个服务:

1、CheckFileInfo服务,此服务会返回文件的基本信息

详情可参考[MS-WOPI] section 3.3.5.1.1 章节

2、GetFile服务,此服务根据上一个服务返回的基本信息返回对应文件的数据流

详情可参考[MS-WOPI] section 3.3.5.3.1 章节。

由于官方文档及上面提供的链接对此说明的很详细,这里就不多说了,我也建议在学习新东西的时候,多看看官方文档。同时,微软员工也给我们提供了一个Wopi Host 的 demo,这里就直接使用他们提供的 demo来学习了。到此处下载该demo:https://code.msdn.microsoft.com/office/Building-an-Office-Web-f98650d6。在demo的 Wopihost 项目中找到 Filecontroller.cs文件,定位到如下代码:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
      
      
/// Returns the metadata about an office document
/// </summary>
/// <param name="name">filename</param>
/// <param name="access_token">access token generated for this server</param>
/// <returns></returns>
[Route( "files/{name}/")]
public CheckFileInfo (string name, string access_token)
{
Validate(name, access_token);
var fileInfo = _fileHelper.GetFileInfo(name);
bool updateEnabled = false;
if ( bool.TryParse(WebConfigurationManager.AppSettings[ "updateEnabled"].ToString(), out updateEnabled))
{
fileInfo.SupportsUpdate = updateEnabled;
fileInfo.UserCanWrite = updateEnabled;
fileInfo.SupportsLocks = updateEnabled;
}
return fileInfo;
}

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
      
      
// GET api/<controller>/5
/// Get a single file contents
/// </summary>
/// <param name="name">filename</param>
/// <returns>a file stream</returns>
[Route( "files/{name}/contents")]
public HttpResponseMessage Get(string name, string access_token)
{
大专栏  应用程序与 Office Web Apps 整合class="keyword">try
{
Validate(name, access_token);
var file = HostingEnvironment.MapPath( "~/App_Data/" + name);
var responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(file, FileMode.Open, FileAccess.Read);
responseMessage.Content = new StreamContent(stream);
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue( "application/octet-stream");
return responseMessage;
}
catch (Exception ex)
{
var errorResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(ex.Message ?? ""));
errorResponseMessage.Content = new StreamContent(stream);
return errorResponseMessage;
}
}

上面的两个方法就是对CheckFileInfo服务和GetFile服务的实现,理解起来并不困难。

由于是一个demo实现,我就直接拿来部署了,等到要改造的时候再去琢磨,打开配置web.config配置文件,修改如下的配置,表示此程序将要部署的地方和别名:

这里写图片描述

访问http://192.168.1.136/hosting/discovery,保存内容到Discovery.xml文件,用此文件替换掉demo中的 WopiHostApp_DataDiscovery.xml文件:

这里写图片描述

我这里直接部署在Office Web Apps所在的服务器上了, 在 HTTP80网站下面添加一个应用程序,别名为dist,与web.config对应上就行了,如下所示:

这里写图片描述

部署成功后,访问 http://192.168.1.136/dist ,打开此demo的主界面,在菜单栏upload页面里面上传一份文档,上传成功后会生成一个链接,访问链接就可以看到刚刚生成的文档了,如下所示:

这里写图片描述

这里写图片描述

由于在web.config 中设置了 updateEnabled=true,所以这里可以对打开的excel进行编辑,如下所示:

这里写图片描述

这个demo就算部署成功了,至于这两个服务具体实现细节,查看代码即可。值得注意的是:这个demo实现并不支持word文档的编辑,作者也进行了说明:

这里写图片描述

The solution and project have been updated to MVC5, and Web API 2. In
addition, editing PowerPoint (PPTX), and Excel files has been added.
Word Editing is not part of the solution. Also, PDF viewing is
enabled.

在代码中位置如下:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
      
      
// POST api/<controller>
/// Not implemented, but will provide editing (simple)
/// </summary>
/// <param name="access_token"></param>
[Route( "files/{name}/contents")]
public async void Post(string name, [FromUri] string access_token)
{
var body = await Request.Content.ReadAsByteArrayAsync();
var appData = HostingEnvironment.MapPath( "~/App_Data/");
var fileExt = name.Substring(name.LastIndexOf( '.') + 1);
var outFile = Path.Combine(
appData,name);
//Guid.NewGuid().ToString() +
//"_" +
//name);
//var fi = new FileInfo(outFile);
File.WriteAllBytes(outFile, body);
}

这里应该是故意不给实现的,要想实现word编辑,就得靠各位的努力了,我也在研究中,希望和大家一起交流交流。当然,你也可以用任何的语言来实现这个程序,只要保证那两个接口能返回相应的文件信息和对应的数据流就行了,至于文件放在何处,用什么语言,这都是小事。

JAVA程序整合可参考我的这篇文章:《Java 程序与 Office Web Apps 整合》

猜你喜欢

转载自www.cnblogs.com/sanxiandoupi/p/11698507.html