This project is read-only.

Fileupload with form post

Topics: Web Api
Dec 16, 2011 at 1:10 PM


I have serious problems getting fileupload to work with big files. I have a service endport that looks something like:

[WebInvoke(UriTemplate = "model", Method = "POST")]
public HttpResponseMessage UploadModel(HttpRequestMessage request)
        if (request.Content.IsMimeMultipartContent())
             var bodyparts = request.Content.ReadAsMultipart();
             var task = bodyparts.FirstDispositionName("model").ReadAsStreamAsync().ContinueWith(t =>
                  var stream = t.Result;
                  using (FileStream fileStream =File.Create(modelPath, (int)stream.Length, FileOptions.None))
                            byte[] bytesInStream =new byte[stream.Length];
                            stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
                            fileStream.Write(bytesInStream, 0, bytesInStream.Length);
         return new HttpResponseMessage(HttpStatusCode.Created);

But whenever I do a standard form post to the endpoint with file bigger than 4MB (not exactly, don't know the exact limit). I get an error.

I am guessing that is is some configuration that is needed for the server to accept big files in post. I have tried to set the MaxReceivedMessageSize=10*1024*1024 in the used HttpConfiguration for the service, but it has no effect.

One more clue. I am testing in the Cassini server. Could this be the issue...

Dec 16, 2011 at 1:59 PM

I found the problem.

I needed to change the web.config as explained in this post:

Hope this saves others from hours of debugging...