Search code examples
asp.net-corefile-uploadlarge-files

Asp.net core file upload by Unobtrusive Ajax not Working


I have an action method:

[RequestSizeLimit(int.MaxValue)]
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)]
[HttpPost]                
[MultipartFormData]
[DisableFormValueModelBinding]        
public async Task<PartialViewResult> Upload(/*[FromQuery] string path*/)
{
...
}

and Partial View:

<form id="frmUpload" method="post" data-ajax="true" data-ajax-method="post" data-ajax-url="@(Url.Action("Upload", "Home"))"
      data-ajax-mode="replace" data-ajax-update="#divUpload" data-ajax-loading="#loading"
      data-ajax-failure="failed" enctype="multipart/form-data">
    <div class="row mt-2">
        <div class="col-md-4">
            <input type="file" name="battlePlans" multiple>
        </div>
        <div>
            <input type="submit" value="Submit" class="btn btn-primary" />
        </div>
    </div>
</form>

@if (ViewBag.Success != null)
{
    <h2 class="alert @(ViewBag.Success ? "alert-success" : "alert-danger")" id="frmMessage">@ViewBag.Message</h2>    
}

and Index.cshtml view:

<div id="divUpload">
    <partial name="IndexPartials/Upload.cshtml" model="null" view-data="@ViewData" />
</div>

@section Scripts {    
    <script>
        $(function () {           
            $.ajax({               
                timeout: 2 * 60 * 60 * 1000, // 2 hour
                maxChunkSize: 1000000 // 1 MB
            });
        });
    </script>
}

Program.cs:

builder.Services.Configure<FormOptions>(options =>
{    
    options.MultipartBodyLengthLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
});

When I upload some small files, files are uploading. But for test, I uploaded a 580MB file that I waited along time, but file upload not completed In IIS Express and VS 2022. (Also when during Run, I put a breakpoint in the loop that reads and writes bytes it hits, and then I deleted the breakpoint and click continue so that executing continues).

No matter how long I wait, the file is not copied completely and only I have some megabytes uploaded, How to correct this?


Solution

  • Try to add a web.config and add the following configuration section,update this limit to support files up to 580MB:

    web.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.webServer>
            <!-- Removed other configuration -->
            <security>
                <requestFiltering>                  
                    <requestLimits maxAllowedContentLength="999217728"/>
                </requestFiltering>
            </security>
        </system.webServer>
    </configuration>
    

    result: enter image description here