Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index while using Request.Files

0

I am trying to create a functionality to upload big files in ASP.NET MVC 5. Here is how my action looks like:

public async Task AddDocument(
    Guid documentId,
    int chunkIndex,
    long totalFileSize,
    long currentChunkSize, 
    int totalChunkCount,
    long chunkByteOffset,
    long chunkSize, 
    string fileName,
    bool chunked,
    Guid? folderId
    )   
{
        var file = Request.Files[0];
        if (file == null) return;
        await _documentService.SaveDocumentsAsync(new PostedFile
        {
            File = file,
            DocumentId = documentId,
            FolderId = folderId,
            Name = fileName,
            Chunk = chunkIndex,
            Chunked = chunked
        });
}

But often I get an exception like below:

at System.Collections.ArrayList.get_Item(Int32 index) at System.Web.HttpFileCollection.Get(Int32 index) at System.Web.HttpFileCollectionWrapper.get_Item(Int32 index) at MyDrive.Areas.Admin.Controllers.HomeController.d__9.MoveNext() in E:\Projects\MyDrive\MyDrive\MyDrive\Areas\Admin\Controllers\HomeController.cs:line 0 --- 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.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass8_0.b__1(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.b__0() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.b__2() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass7_0.b__1(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_6.b__4() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.b__1(IAsyncResult asyncResult)

I could only see this happening when the app is deployed live. I guess it fails internally when I try to read from the request but I have no idea how can i solve this issue. I am using dropzone.js to chunk files in client side like below:

dropZone.options.params = function(files, xhr, chunk) {
    if (chunk) {
        return {
            documentId: chunk.file.upload.uuid,
            chunkIndex: chunk.index,
            totalFileSize: chunk.file.size,
            currentChunkSize: chunk.dataBlock.data.size,
            totalChunkCount: chunk.file.upload.totalChunkCount,
            chunkByteOffset: chunk.index * this.options.chunkSize,
            chunkSize: this.options.chunkSize,
            filename: chunk.file.name,
            chunked: true,
            folderId: '@(ViewBag.Id)'
        };
    } else {
        return {
            documentId: files[0].upload.uuid,
            chunkIndex: 0,
            totalFileSize: files[0].size,
            currentChunkSize: 0,
            totalChunkCount: 0,
            chunkByteOffset: 0,
            chunkSize: 0,
            filename: files[0].name,
            chunked: true,
            folderId: '@(ViewBag.Id)'
        };
    }
};

The problem does not happen always, it might happen one time in 20 uploaded documents, furthermore could not distinguish anything specific from the failed documents why they might be causing it. Also its difficult to debug as I never seen this happening locally, I can only see at my logs.

Doe anybody know any reason why this might happen ?

Trying time after time to see if I find anything and at some point I got another error:

An error occurred while communicating with the remote host. The error code is 0x80070001.Incorrect function. (Exception from HRESULT: 0x80070001)

at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) at System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size) at System.Web.Hosting.IIS7WorkerRequest.ReadEntityBody(Byte[] buffer, Int32 size) at System.Web.HttpRequest.GetEntireRawContent() at System.Web.HttpRequest.GetMultipartContent() at System.Web.HttpRequest.FillInFormCollection() at System.Web.HttpRequest.EnsureForm() at System.Web.HttpRequest.get_Form() at System.Web.HttpRequestWrapper.get_Form() at System.Web.Mvc.FormValueProvider..ctor(ControllerContext controllerContext, IUnvalidatedRequestValues unvalidatedValues) at System.Web.Mvc.FormValueProviderFactory.GetValueProvider(ControllerContext controllerContext) at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) at System.Web.Mvc.ControllerBase.get_ValueProvider() at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.b__0(AsyncCallback asyncCallback, Object asyncState)

Not sure if these two might be related to the same problem :(

c#
asp.net
asp.net-mvc
asynchronous
asked on Stack Overflow Mar 3, 2019 by Rajmond Burgaj • edited Mar 3, 2019 by Rajmond Burgaj

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0