最近我和我的团队实施了一个WOPI-Host,支持查看和编辑Word,PPT和Excel文档.您可以查看https://github.com/marx-yu/WopiHost,这是一个命令提示项目,可以监听8080端口并通过Microsoft Office Web Apps编辑和查看word文档.
我们已经在webApi中实现了这个解决方案,它运行良好.希望这个示例项目能够帮到你.
在请求之后,我将尝试添加代码示例以阐明基于我的webApi实现实现它的方法,但是他们需要实现很多代码来实际使其正常工作.
首先,要启用编辑,您需要在FilesController中捕获Http Posts.与实际编辑有关的每个帖子都会有标题X-WOPI-Override等于COBALT.在这些帖子中,您将发现InputStream是Atom类型.根据MS-WOPI文档,在您的回复中,您需要包含以下标题X-WOPI-CorrelationID和request-id.
这是我的webApi post方法的代码(因为我还在实现WOPI协议,所以它还没有完成).
string wopiOverride = Request.Headers.GetValues("X-WOPI-Override").First();
if (wopiOverride.Equals("COBALT"))
{
string filename = name;
EditSession editSession = CobaltSessionManager.Instance.GetSession(filename);
var filePath = HostingEnvironment.MapPath("~/App_Data/");
if (editSession == null){
var fileExt = filename.Substring(filename.LastIndexOf('.') + 1);
if (fileExt.ToLower().Equals(@"xlsx"))
editSession = new FileSession(filename, filePath + "/" + filename, @"yonggui.yu", @"yuyg", @"yonggui.yu@emacle.com", false);
else
editSession = new CobaltSession(filename, filePath + "/" + filename, @"patrick.racicot", @"Patrick Racicot", @"patrick.racicot@hospitalis.com", false);
CobaltSessionManager.Instance.AddSession(editSession);
}
//cobalt, for docx and pptx
var ms = new MemoryStream();
HttpContext.Current.Request.InputStream.CopyTo(ms);
AtomFromByteArray atomRequest = new AtomFromByteArray(ms.ToArray());
RequestBatch requestBatch = new RequestBatch();
Object ctx;
ProtocolVersion protocolVersion;
requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion);
editSession.ExecuteRequestBatch(requestBatch);
foreach (Request request in requestBatch.Requests)
{
if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content)
{
//upload file to hdfs
editSession.Save();
}
}
var responseContent = requestBatch.SerializeOutputToProtocol(protocolVersion);
var host = Request.Headers.GetValues("Host");
var correlationID = Request.Headers.GetValues("X-WOPI-CorrelationID").First();
response.Headers.Add("X-WOPI-CorrelationID", correlationID);
response.Headers.Add("request-id", correlationID);
MemoryStream memoryStream = new MemoryStream();
var streamContent = new PushStreamContent((outputStream, httpContext, transportContent) =>
{
responseContent.CopyTo(outputStream);
outputStream.Close();
});
response.Content = streamContent;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentLength = responseContent.Length;
}
Run Code Online (Sandbox Code Playgroud)
正如您在此方法中所看到的,我使用了CobaltSessionManager和CobaltSession用于在Cobalt协议上创建和管理编辑会话的方法.您还需要一个我称之为CobaltHostLockingStore的东西,用于在版本初始化中与Office Web App服务器通信时处理不同的请求.
我不会发布这3个类的代码,因为它们已经在我发布的示例github项目中编码,并且即使它们很大,它们也很容易理解.
如果您有更多问题或者不够清楚,请不要犹豫,我会相应地更新我的帖子.