webapi核心无法上传被cors策略阻止的大文件 [英] webapi core unable to upload large file blocked by cors policy

查看:355
本文介绍了webapi核心无法上传被cors策略阻止的大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将大型文件从Angular 9.0前端上传到Web Api .net core 3.1后端。
我正在关注本教程 https:// code- maze.com/upload-files-dot-net-core-angular/
在默认的最大文件大小为28.6 mb的情况下,所有这些都可以正常工作,一旦超过该文件大小,我会收到以下cors错误:'已阻止从源[localhost]在[localhost]对XMLHttpRequest的访问CORS策略:从服务器返回的请求资源上没有 Access-Control-Allow-Origin标头。我尚未在IIS上尝试过它,因此我在本地使用Kestrel遇到了这些问题。



我的ConfigureServices看起来像这样:

  services.AddCors(options => 
{
options.AddPolicy( AllowLocalAngularWebClient,
policy => policy.WithOrigins ( http:// localhost:4200).WithHeaders()。AllowAnyHeader()。AllowAnyMethod());
});

我的配置如下:

  app.UseCors( AllowLocalAngularWebClient); 
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider
(Path.Combine(Directory.GetCurrentDirectory(),@ uploads)),
RequestPath = new PathString( / uploads)
});

我的控制器如下:

  [EnableCors( AllowLocalAngularWebClient)] 
[DisableRequestSizeLimit]

我遇到过一些博客,说我需要配置红est,所以我在BuildWebHost中尝试过:

 。 ConfigureKestrel((context,options)=> 
{
options.Limits.MaxRequestBodySize = 52428800; //或null表示无限制!
})

在我的控制器上,我也尝试过两者:

  [DisableRequestSizeLimit] 

  [RequestSizeLimit(52428800)] 

但是我仍然可以得到文件高于28.6mb的错误



我不知道这是cors错误还是本地IIS /红est Visual Studio错误。



任何帮助将不胜感激。


解决方案

我应该为此添加更多...



我的解决方案是更新IISExpress配置,将以下行添加到IISExpress配置文件中:

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

关键在于:

 < requestLimits maxAllowedContentLength = 500000000>< / requestLimits> 

对于我来说,要更新的配置文件位于C:\Users\USER_NAME\ source\repos\PROJECT_NAME> .vs\PROJECT_NAME\config\applicationhost.config



您可以通过启动项目(运行IISExpress)找到配置文件。 ,右键单击系统任务栏中的IISExpress图标,单击显示所有应用程序,然后选择您的特定站点/项目。



我希望这对某人有帮助,我烧了几个小时


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.

解决方案

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.

这篇关于webapi核心无法上传被cors策略阻止的大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆