Error receiving multipart form via WebAPI

2

how are you?

I created a form in Angular2, which makes a POST request with the form data and upload a file, to a webapi controller. However I'm having a little problem.

Some users of my system (who access the system outside my company's network) in 20% attempts they submit the form, an error happens (I registered with NLog). However, if I try to submit the form with the same data and the same files, within the company network, everything works perfect (100% of the attempts).

However the error is intermittent, it happens on average 2 out of 10 requests, and only with users who access the system outside the company network.

And my C # controller: `public async Task PostFormData() { HttpResponseMessage response;

    try
    {
            if (!Request.Content.IsMimeMultipartContent())
            {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            var fileContent = provider.Contents.FirstOrDefault(x => x.Headers.ContentType != null &&
                                    (x.Headers.ContentType.MediaType.StartsWith("image/") ||
                                     x.Headers.ContentType.MediaType.Equals("application/pdf")));

            // Function to transform form content in object                         
            var transaction = await FormObject<Product>(provider);

            // Performs data processing, etc...
     }

}`

The error happens on the line:

Await Request.Content.ReadAsMultipartAsync (provider);

The file upload is limited to 5Mb (in Angular2), and in WebConfig I have limited the size of the request and the upload to 30Mb (to leave a certain slack).

The exception that happens is the following:

StackTrace: at System.Net.Http.HttpContentMultipartExtensions.d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpContentMultipartExtensions.d__0`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Sisprec.Api.Controllers.TransacaoController.d__12.MoveNext()

InnerException: System.Web.HttpException (0x800703E3): The client disconnected. at System.Web.Hosting.IIS7WorkerRequest.EndRead(IAsyncResult asyncResult) at System.Web.HttpBufferlessInputStream.EndRead(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncTrimPromise1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpContentMultipartExtensions.d__8.MoveNext()

And now I do not know what to do.

Thank you very much for all the help.

c#
asp.net
angular
asp.net-web-api
file-upload
asked on Stack Overflow Mar 23, 2017 by Wesley

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0