I have a zip
file that I can read with DotNetZipLib from the file system. However, when I POST it via a form
to my MVC application it can't be read as a stream. My best guess at the moment is that the HTTP upload is somehow corrupting the zip
file. There's no shortage of questions with the same problem, and I thought I'd accounted for the stream properly but perhaps I'm not using the .NET object(s) here as intended.
Here's my WebAPI POST handler:
public void Post(HttpRequestMessage request)
{
using(var fileData = request.Content.ReadAsStreamAsync().Result)
if (fileData.Length > 0)
{
var zip = ZipFile.Read(fileData); // exception
}
}
The exception, of course, is from the DotNetZipLib ZipFile
just saying that the stream can't be read as a zip
. If I replace fileData
with just a path to the file (this is all being tested on the same machine) then it reads it, so it has to be the HTTP upload.
In FireBug, the headers for the POST are:
Response Headers:
Cache-Control no-cache
Content-Length 1100
Content-Type application/xml; charset=utf-8
Date Sat, 01 Feb 2014 23:18:32 GMT
Expires -1
Pragma no-cache
Server Microsoft-IIS/8.0
X-AspNet-Version 4.0.30319
X-Powered-By ASP.NET
X-SourceFiles =?UTF-8?B?QzpcRGF0YVxDb2RlXE9yZ1BvcnRhbFxPcmdQb3J0YWxTZXJ2ZXJcYXBpXGFwcHg=?=
Request Headers
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Cookie uvts=ENGUn8FXEnEQFeS
Host localhost:48257
Referer http://localhost:48257/Home/Test
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
Request Headers From Upload Stream
Content-Length 31817
Content-Type multipart/form-data; boundary=---------------------------265001916915724
And the form
is simple enough:
<form action="/api/appx" method="post" enctype="multipart/form-data">
<input name="postedFile" type="file" />
<input type="submit" />
</form>
Am I doing something wrong with the steam? Pulling data from the HttpRequestMessage
incorrectly? Or perhaps I should be receiving the upload in an entirely different way?