其想法是使用Office Web Apps构建一个专有的Java后端文档系统。
我们已经创建了WOPI客户端,它允许我们查看/编辑PowerPoint和Excel web应用程序文档,但我们只能查看Word文档。
为了编辑Word Web App文档,您需要实现MS-FSSHTTP。
似乎没有关于如何在代码中实际做到这一点的信息。有没有人这样做过,或者知道怎么做?
发布于 2014-08-06 20:50:01
最近,我和我的团队实现了一个支持查看和编辑Word、PPT和Excel文档的WOPI-Host。您可以看看https://github.com/marx-yu/WopiHost,这是一个命令提示符项目,它监听8080
端口,并支持通过Microsoft Office Web Apps编辑和查看word文档。
我们已经在webApi中实现了这个解决方案,它工作得很好。希望这个样例项目能帮到你。
在收到请求后,我将尝试添加代码示例,以阐明如何基于我的webApi实现来实现它,但要使它真正正常工作,需要实现大量代码。
首先,要启用编辑,您需要在FilesController中捕获Http Posts。涉及实际编辑的每个帖子的标题X-WOPI-Override
都等于COBALT
。在这些帖子中,你会发现InputStream是and Atom类型。根据MS-WOPI文档,您需要在响应中包含以下标头X-WOPI-CorrelationID
和request-id
。
下面是我的WOPI方法的代码(它并不完整,因为我还在实现webApi协议)。
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;
}
正如您在此方法中所看到的,我使用了CobaltSessionManager
和CobaltSession
,它们用于创建和管理基于Cobalt协议的编辑会话。您还需要一个我称之为CobaltHostLockingStore的东西,它用于在版本初始化中与Office Web App服务器通信时处理不同的请求。
我不会发布这3个类的代码,因为它们已经在我发布的示例github项目中编写了代码,而且即使它们很大,它们也很容易理解。
如果你有更多的问题或者不够清楚,请不要犹豫地发表意见,我会相应地更新我的帖子。
发布于 2015-02-13 21:40:08
Patrick Racicot,提供了很好的答案。但是我在保存docx时遇到了问题(CobaltCore.dll中的异常),我甚至开始使用dotPeak反射器来尝试解决这个问题。
但是在我的WebApi方法中锁定了editSession
变量之后,一切都开始变得神奇起来。看起来OWA发送的请求应该作为一个链来处理,而不是像通常的控制器方法那样并行处理。
https://stackoverflow.com/questions/17280077
复制相似问题