0

I have a Web API 2 project which accepts file uploads. Uploading small files works pretty well, but when it comes to large files, they simply are not written to disk. It appears to me that this happens with files that are somewhere around 2 GB. Files of about 1 gb upload pretty well.

These are the relevant parts of my web.config:

<system.web>
  <compilation debug="true" targetFramework="4.6.1" /> 
  <httpRuntime targetFramework="4.6.1" maxRequestLength="2147483647"  />
</system.web>

...

<system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295" />
      </requestFiltering>
    </security>
  </system.webServer>

I got the values for maxAllowedContentLength and maxRequestLength from this answer.

Since I am running IIS 8 I would expect to meet the maxAllowedContentLength value.

And this is the Action responsible for handling the upload, based on this blog post by Filip W:

    [HttpPost]
    public async Task<HttpResponseMessage> Post()
    {
        //Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string uploadPath = @"C:\temp\upload"; // in reality this is retrieved from DB

        try
        {
            var provider = new MultipartFormDataStreamProvider(uploadPath);
            var content = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
            foreach (var header in Request.Content.Headers)
            {
                content.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }

            var result = await content.ReadAsMultipartAsync(provider);

            return Request.CreateResponse(HttpStatusCode.Accepted);
        }
        catch(Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }

    }

Uploading small files works. I see them written to c:\temp\upload. But large files are not saved, although I see them coming in (by watching Task Manager > Performance:

20 Mbps upload on the server

Does anyone have an idea why I don't see large requests saved to disk, while smaller files do get saved?

Community
  • 1
  • 1
jao
  • 18,273
  • 15
  • 63
  • 96
  • 1
    Did you check the server logs? Did you try to save 1.9gb file? Sometimes though its 2gb max upload limit, but header and footers create problem. – Ritesh May 31 '16 at 18:01
  • There's nothing in the event log, unfortunately. I didn't check for 1.9 GB yet because I haven't got a 1.9 GB file. I will try to test that later – jao May 31 '16 at 18:33

0 Answers0