zoukankan      html  css  js  c++  java
  • 如何整合Office Web Apps至自己开发的系统(二)

    WOPI项目的创建

    首先用vs2012创建一个mvc4的程序。如图:

    clip_image002

    从上一篇我们可以知道,WOPI通讯主要通过两个服务:

    一个是CheckFileInfo服务,

    一个是GetFile服务。

    所以下面我们主要介绍这两个服务的创建。

    1. 首先创建CheckFileInfo服务:

    我们先确定这个服务的路由地址

    设置为:http://<ServerName>/files/<filename>?access_token=<token>

    修改App_Start文件夹下面的WebApiConfig.cs文件。

    插入下列代码:

    config.Routes.MapHttpRoute(

    name: "FileInfo",

    routeTemplate: "wopi/files/{name}",

    defaults: new { controller = "files", action = "GetFileInfo" }

    );

    如图所示

    clip_image004

    创建一个名称为files的Controller,

    clip_image006

    设置为空API控制器:

    clip_image008

    之所以我们不用平常的MVC控制器,而选API控制器,是因为我们做的是服务,来返回信息,所以要换成ApiController。

    这个服务要返回的是json,属性包括为

    BaseFileName

    OwerId

    Size

    SHA256

    Version

    所以我们要创建一个model,包含上述属性

    如下图:

    clip_image010

    在上述的路由器规则中,action用的是GetFileInfo方法,所以要在FileController规则中写一个GetFileInfo方法,这个方法返回CheckFileInfo类型。

    public CheckFileInfo GetFileInfo(string name, string access_token)
    
    {
    
    string _access_token = access_token;
    
    var file = HostingEnvironment.MapPath("~/App_Data/" + name);//从硬盘中获取name文件
    
    FileInfo info = new FileInfo(file);
    
    var json = new CheckFileInfo
    
    {
    
    BaseFileName = info.Name ,//"test.docx",
    
    OwnerId = "admin",
    
    Size = info.Length,
    
    SHA256 = "+17lwXXN0TMwtVJVs4Ll+gDHEIO06l+hXK6zWTUiYms=",
    
    Version = "GIYDCMRNGEYC2MJREAZDCORQGA5DKNZOGIZTQMBQGAVTAMB2GAYA===="
    
    };
    
    return json;
    
    }

    如下图

    clip_image012

    我们访问一下这个地址:

    http://192.9.206.52:1407/wopi/files/test.docx?access_token=06l+hXK6zWTUi

    这个192.9.206.52是我的本机地址。

    得到下列结果:

    clip_image014

    证明这个服务制作成功。

    2.然后再来制作GetFile服务。

    因为GetFileInfo的URI地址

    http://<ServerName>/files/<filename>?access_token=<token>

    所以GetFile地址应该比其多一个/Contents,所以为

    http://<ServerName>/files/<filename>/Contents?access_token=<token>

    设置它的路由地址

    config.Routes.MapHttpRoute(

    name: "Contents",

    routeTemplate: "wopi/files/{name}/contents",

    defaults: new { controller = "files", action = "GetFile" }

    );

    如下图:

    image

    GetFile这个服务返回的应该是数据流,所以返回的类型应该是HttpResponseMessage类型。

    从硬盘中获取一个doc文件,转换为Stream类型,代码如下:

    public HttpResponseMessage GetFile(string name, string access_token)
    
    {
    
    try
    
    {
    
    string _access_token = access_token;
    
    var file = HostingEnvironment.MapPath("~/App_Data/" + name);//name是文件名
    
    var rv = new HttpResponseMessage(HttpStatusCode.OK);
    
    var stream = new FileStream(file, FileMode.Open, FileAccess.Read);
    
    rv.Content = new StreamContent(stream);
    
    rv.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
    return rv;
    
    }
    
    catch (Exception ex)
    
    {
    
    var rv = new HttpResponseMessage(HttpStatusCode.InternalServerError);
    
    var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(ex.Message ?? ""));
    
    rv.Content = new StreamContent(stream);
    
    return rv;
    
    }
    
    }

    如下图:

    clip_image018

    至此,两个服务制作完毕。

    可以访问下列地址查看效果,

    http://192.9.206.50/wv/wordviewerframe.aspx?WOPISrc=http%3a%2f%2f192.9.206.52%3a1407%2fwopi%2ffiles%2ftest.docx&access_token=06l+hXK6zWTUi

    其中

    192.9.206.50为OWA的机器地址,

    192.9.206.52为本机的地址。

    这个URL地址带了两个参数

    分别为WOPISrc,值为http://192.9.206.52:1407/wopi/files/test.docx

    Access_token,值为06l+hXK6zWTUi

    这两个参数的意思,我已经在以前的博文中《如何整合Office Web Apps至自己开发的系统(一)》说过了。

    在这个例子中,access_token我是随便取的,并且在代码中也没有对这个令牌进行验证。

    确保两台机器的相应端口能互相访问。

    访问得到的结果如下:

    clip_image020

    怎么会访问出错呢?

    翻了很久资料,发现有老外也遇到过类似这种问题:

    I write this message because on actually working on this WOPI protocol. I try to build a WOPI host. I think i'm almost finish the "view" action. But i got some problems with the CheckFileInfo (JSON) or GetFile (/content). For me everything is well fonctionning, but the WAC doesn't work just after it call my JSON. I really dont know why.. I observed all the interactions between SharePoint and WAC, to show what is different with mine host. But i think i need some help now. Does anyone can try to give me some hint ? I checked everythings (Correlation-ID, JSON, access-token) ...

    别人的回答是让他考虑一下是不是SHA散列算法的问题:

    You might also double-check that your SHA hashes are being calculated correctly - this can cause some problems.

    并给了一个网站地址:www.tylerbutler.com/.../base64-encoded-sha256-hashes

    那就按照提示把散列算法加上去,

    代码如下:

    var file = HostingEnvironment.MapPath("~/App_Data/" + name);//从硬盘中获取name文件
                FileInfo info = new FileInfo(file);
    
                var hasher = SHA256.Create();
                byte[] hashValue;
                using (Stream s = File.OpenRead(file))
                {
                    hashValue = hasher.ComputeHash(s);
                }
                string sha256 = Convert.ToBase64String(hashValue);

    如下图:

    clip_image022

    再次运行,OK,大功告成

    clip_image024

    其实按照上述步骤,就可以在自己的系统中调用Office Web Apps的查看功能了,实在要看demo的同学可以去下列链接下载

    http://download.csdn.net/detail/poisson1984/6003183

    最近csdn上的积分吃紧,顺便刷点积分,微笑

    下面有一个外国的例子,会更全面:http://pan.baidu.com/s/1f4suc

    因为所在公司发展方向的原因,没有太多时间继续深入研究OWA,敬请见谅(2016-05-05)

  • 相关阅读:
    C# .net 中文简体繁体转换
    关于html+js将值传输到后端C#的问题(删减版)
    Error mapping types
    Vs编译时RazorTagHelper
    Sum All Numbers in a Range-freecodecamp算法题目
    Caesars Cipher-freecodecamp算法题目
    Where do I belong-freecodecamp算法题目
    Seek and Destroy-freecodecamp算法题目
    Falsy Bouncer-freecodecamp算法题目
    Mutations-freecodecamp算法题目
  • 原文地址:https://www.cnblogs.com/poissonnotes/p/3277280.html
Copyright © 2011-2022 走看看