Open and save office documents with WebApi
I had done some research but it's beyond my understanding. I try to catch
the request/response with fiddler. I made some progress to get 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 offer to open the file, howerver the name is '5', so this part
still not work correctly.
The request is simple a get (started by the borwser) 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 when I try
to save from word according to fiddler the following happends:
First a request go out with the following content:
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
Of course the response is 404:
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles:
=?UTF-8?B?QzpcV29ya1xSZXNlYXJjaFxXZWJBcGlUb09mZmljZVxXZWJBcGlUb09mZmljZVxhcGlcV29yZEFwaVw=?=
X-Powered-By: ASP.NET
Date: Wed, 21 Aug 2013 07:47:01 GMT
Content-Length: 200
Then another request goes out:
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
I do not implement a method named Head so the answer is 405-Method no
allowed.
Then the last try of the office:
OPTIONS http://localhost:49590/api/ 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
Also result 404 of course
On the second try I implement the Head and Options but it's just resturn
200-OK. The result are the same. It's try the /api/WordApi/ then
/api/WordApi/5 then /api/.
Any idea how to implement my webapi to succesfully communicate with office
word? I have to make it with webapi itself. The storage of the files is
irrelevan, just the interface between the office and the server is
important. I can't use sharepoint or other document storage technology.
Thanks,
Péter
No comments:
Post a Comment