3

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.

  • https://stackoverflow.com/questions/4022434/how-to-set-the-maxallowedcontentlength-to-500mb-while-running-on-iis7 – S_Abolfazli Jun 24 '20 at 04:52
  • Click https://stackoverflow.com/questions/4022434/how-to-set-the-maxallowedcontentlength-to-500mb-while-running-on-iis7 as well. – S_Abolfazli Jun 24 '20 at 04:58

2 Answers2

1

I found the problem after stripping back my whole project and wanted to share this in case anyone else has the same problem. You need to have the commandName option in launchSettings.json set to 'Project' so it is executed with the .NET CLI directly on the command line. If the commandName is set to 'IISExpress' the upload is limited to 28.6 mb.

0

Thought I would add a bit more to this...

My solution was to update the IISExpress config, adding the following line to the IISExpress config file:

  <requestFiltering>
    <requestLimits maxAllowedContentLength="500000000"></requestLimits>
    <fileExtensions allowUnlisted="true" applyToWebDAV="true">

The key line being:

<requestLimits maxAllowedContentLength="500000000"></requestLimits>

In my case, the config file to update was at C:\Users\USER_NAME\source\repos\PROJECT_NAME>.vs\PROJECT_NAME\config\applicationhost.config

You can find the config file by starting your project (running IISExpress), right click the IISExpress icon in the system tray, click 'Show All Applications' and select your specific site/project.

I hope this helps someone, I burnt several hours on this.

Ben Halicki
  • 60
  • 1
  • 1
  • 5