問題描述
使用 WebApi 打開辦公文檔並使用 MS Office 保存回站點 (Open office documents with WebApi and save back to the site using MS Office)
I have done some research but this is beyond my understanding. I am using Fiddler to try to catch the request/response. The goal would be that this webapi behave like a sharepont document repository without so much resource and license. I have made some progress getting the file:
public HttpResponseMessage Get(int id)
{
byte[] content = null;
using (FileStream fs = File.Open(@"d:\some.docx", FileMode.Open))
{
content = new byte[fs.Length];
fs.Read(content, 0, (int)fs.Length);
}
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(content);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
{
FileName = "some.docx"
};
return response;
}
The browser offers to open the file, however the name is 5
, so this part still doesn't work properly. The request is simple to get, being started by the browser, and the response looks like this (without the content):
HTTP/1.1 200 OK
Cache‑Control: no‑cache
Pragma: no‑cache
Content‑Length: 403170
Content‑Type: application/msword
Expires: ‑1
Server: Microsoft‑IIS/8.0
Content‑Disposition: inline; filename=some.docx
X‑AspNet‑Version: 4.0.30319
X‑SourceFiles: =?UTF‑8?B?QzpcV29ya1xSZXNlYXJjaFxXZWJBcGlUb09mZmljZVxXZWJBcGlUb09mZmljZVxhcGlcV29yZEFwaVw1?=
X‑Powered‑By: ASP.NET
Date: Wed, 21 Aug 2013 08:27:02 GMT
I access the file via url: http://localhost:49590/api/WordApi/5
.
In the meantime I found the following article: http://support.microsoft.com/kb/838028 I admit I don't entirely understand it, but I think I should implement a WebDAV
in my application? I tried to add some options in the hope of something changing in the request made by Office, like so:
public HttpResponseMessage Options()
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.Add("MS‑AUTHOR‑VIA", new[] { "LOCK", "UNLOCK", "PROPPATCH" });
return response;
}
But unfortunately nothing changed. The Office request was:
OPTIONS http://localhost:49590/api/WordApi/ HTTP/1.1
X‑IDCRL_ACCEPTED: t
User‑Agent: Microsoft Office Protocol Discovery
Host: localhost:49590
Content‑Length: 0
Connection: Keep‑Alive
Pragma: no‑cache
And afterwards it made another one:
HEAD http://localhost:49590/api/WordApi/5 HTTP/1.1
X‑IDCRL_ACCEPTED: t
User‑Agent: Microsoft Office Existence Discovery
Host: localhost:49590
Content‑Length: 0
Connection: Keep‑Alive
Pragma: no‑cache
Is there any way I can trick Office into sending my web API the file upon saving? Another interesting thing, when I was debugging, in the application the methods Options()
and Head(int id, [FromBody] string value)
are called twice but according to Fiddler the request was only sent once in both cases above.
Any idea how to implement this open/save mechanism using a webapi?
UPDATE:
Looks like everybody missed the point. The problem is not to open the document, but to save it locally using Office (not to upload a page in the application). If I use an attachment as Content‑Disposition the browser opens the file in a different way. It saves the file to the temp and opens the document from that location. In this case Office is not even try to connect to my webapi. However if I use inline Office opens the document from the website location and checks the capabilities of my webapi through the OPTIONS and HEAD requests. The browser produces the following open dialog box: If set to attachment: If set to inline:
參考解法
方法 1:
The answer is painfully. The solution is to implement WebDAV in a custom handler.