Asp.net与office web apps的整合

发布时间:2017-09-04 12:11:47
Asp.net与office web apps的整合 其实网上有关office web app的整合已经有相关的文章了,典型的是如何整合Office Web Apps至自己开发的系统(一) 和如何整合Office Web Apps至自己开发的系统(二),微软官网也有相应的demo。

这里在简单描述一下原理吧:office web apps(owas)扮演者一个客服端,它会访问我们asp.net 站点的文件然后呈现出来。而我们常用的API主要有如下3个:

GET api/wopi/files/{name}?access_token={access_token}

GET api/wopi/files/{name}/contents?access_token={access_token}

POST api/wopi/files/{name}/contents?access_token={access_token}

至于每个API做什么 这里就不多说,第一个是owas 检查文件,专题系统,传递的信息是json数据格式,第二个是owas获取文件流,第三个是owas post的文件流(保存修改文件)。首先我们来看看第一个API的实现:

复制代码

[Route("files/{name}/")]

public CheckFileInfo GetFileInfo(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;

}

复制代码

这里的 Validate(name, access_token) 方法主要是验证请求的文件名name与参数access_token是否一致,主要是验证是否是非法访问,返回一个CheckFileInfo对象,CheckFileInfo的定义如下:

复制代码

public class CheckFileInfo

{

public CheckFileInfo()

{

this.SupportsUpdate = false;

this.UserCanWrite = false;

}

public string BaseFileName { get; set; }

public string OwnerId { get; set; }

public long Size { get; set; } //in bytes

public string SHA256 { get; set; } //SHA256: A 256 bit SHA-2-encoded [FIPS180-2] hash of the file contents

public string Version { get; set; } //changes when file changes.

public bool SupportsUpdate { get; set; }

public bool UserCanWrite { get; set; }

public bool SupportsLocks { get; set; }

}

复制代码

现在在来看看第二个api的实现,主要返回对应文件的数据流:

复制代码

[Route("files/{name}/contents")]

public HttpResponseMessage Get(string name, string access_token)

{

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;

}

}

复制代码

而第三个api是将返回的数据流保存到物理文件:

复制代码

[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);

File.WriteAllBytes(outFile, body);

}

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站建设公司 https://www.jingchucn.com/zt/wuhan_wangzhanjianshe/