I am trying to upload large files from an Angular 9.0 front end to a Web Api .net core 3.1 back end. I am following this tutorial https://code-maze.com/upload-files-dot-net-core-angular/. It all works perfect up to the default max file size of 28.6 mb, once I go above this file size I get the following cors error: 'Access to XMLHttpRequest at [localhost] from origin [localhost] has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.' returned from the server. I haven't tried it yet on IIS so I am getting these problems locally with Kestrel.
My ConfigureServices looks like this:
services.AddCors(options =>
{
options.AddPolicy("AllowLocalAngularWebClient",
policy => policy.WithOrigins("http://localhost:4200").WithHeaders().AllowAnyHeader().AllowAnyMethod());
});
My Configure looks like this:
app.UseCors("AllowLocalAngularWebClient");
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider
(Path.Combine(Directory.GetCurrentDirectory(), @"uploads")),
RequestPath = new PathString("/uploads")
});
My controller looks like this:
[EnableCors("AllowLocalAngularWebClient")]
[DisableRequestSizeLimit]
I have come across a few blogs saying I need to configuring kestrel, so I have tried this in BuildWebHost:
.ConfigureKestrel((context, options) =>
{
options.Limits.MaxRequestBodySize = 52428800; //or null for unlimited!
})
Also on my controller I have tried both:
[DisableRequestSizeLimit]
and
[RequestSizeLimit(52428800)]
But I still get the cors error with files above 28.6mb
I don't know if this is a cors error or local IIS / kestrel visual studio error.
Any help would be greatly appreciated.