如何整合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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM