如何整合Office Web Apps至本身開發的系統(二)

WOPI項目的建立html

首先用vs2012建立一個mvc4的程序。如圖:算法

clip_image002

從上一篇咱們能夠知道,WOPI通信主要經過兩個服務:json

一個是CheckFileInfo服務,mvc

一個是GetFile服務。app

因此下面咱們主要介紹這兩個服務的建立。網站

 

1. 首先建立CheckFileInfo服務:ui

咱們先肯定這個服務的路由地址this

設置爲:http://<ServerName>/files/<filename>?access_token=<token>spa

 

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

插入下列代碼:

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)

相關文章
相關標籤/搜索