0

I've been having this problem all day, and i'm pretty lost as to why it happens.

I'm posting a file (a .xls file) from the client side in a React JS component in response to a click on a button like this:

upload: function (e) {
    e.preventDefault();
    var model = this.state.model;
    var xlsFile = this.refs.xlsFile.getDOMNode();
    var file = xlsFile.files[0];
    var fd = new FormData();
    fd.append('file', file);
    var request = {
        groupId: this.state.vkNumber,
        payload: fd
    };
    model.importRequest = request;
    model.setImportProcessing();
    MultiOrderActions.updateUsers(request);

    this.setState(model.toState());
}

The request as it appears in Chrome (POST-request):

Accept:/ Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8,da;q=0.6 Connection:keep-alive Content-Length:3799243 Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryBVYYdSXRza1yRuW4

And the payload of the request:

------WebKitFormBoundaryBVYYdSXRza1yRuW4 Content-Disposition: form-data; name="file"; filename="bruger_caas.xls" Content-Type: application/vnd.ms-excel

------WebKitFormBoundaryBVYYdSXRza1yRuW4--

Should there not be any binary data in the request? The content length is correct, and so is the content-type and filename.

In WebAPI i'm doing the following, where the ReadAsMultiPartAsync never returns:

[HttpPost]
    [Route("bc/s/{*url}")]
    public async Task<IHttpActionResult> Post(string url)
    {
        var service = CreateClient<GenericService>();
        if (Request.Content.IsMimeMultipartContent())
        {
            try
            {
                var provider = new MultipartMemoryStreamProvider();
                await Request.Content.ReadAsMultipartAsync(provider);

                foreach (var content in provider.Contents)
                {
                    //Do stuff
                    ;
                }

            }
            catch (Exception e)
            {
                ;
            }
        }

        return InternalServerError();
    }

When it reaches the await keyword it just dies, and times out after 2 minutes, because Chrome cancels the request.

What am i doing wrong here? I'm really not to sure if the problem is that the file is never really sent from the client side, or if it's my server side code that's the problem.

Any input would be much appreciated.

Rylander86
  • 51
  • 4

1 Answers1

0

for those googling... I was also troubleshooting timeouts for file uploads. In my case, I was getting a deadlocked thread when using the MultipartMemoryStreamProvider and ReadAsMultipartAsync

In the end this other post had a solution for me:

Request.Content.ReadAsMultipartAsync never returns

which uses "TaskCreationOptions.LongRunning" to ensure a distinct thread and prevent a deadlock. The comments of that issue have a helpful explanation.

DavB.cs
  • 579
  • 5
  • 6