webapi core unable to upload large file blocked by cors policy

我是研究僧i 提交于 2020-07-10 06:07:29

问题


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.


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/60617821/webapi-core-unable-to-upload-large-file-blocked-by-cors-policy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!