【问题标题】:CORS failure when uploading large files in ASP.Net Core在 ASP.Net Core 中上传大文件时出现 CORS 失败
【发布时间】:2021-06-23 19:46:45
【问题描述】:

我有一个接收大文件的 ASP.Net Core 3.1 服务。我已经配置了RequestSizeLimitMultipartBodyLengthLimit,但是只有超过 30MB 的文件才会出现 CORS 错误。该上传下的文件没有问题。我正在使用 ISS express 和 Azure App Services Windows 主机在本地遇到这种情况。

[RequestSizeLimit(525336576)]//501MB
[RequestFormLimits(MultipartBodyLengthLimit = 524288000)]//500MB, which is already too high
[HttpPost("Video/{id:guid}")]
public IActionResult SetVideo(Guid id, IFormFile file)
{
   ...
}

Chrome 错误:

Access to XMLHttpRequest at 'https://localhost:44331/v1/Media/Video/a8a221ed-e111-42ad-aac7-d1fa380e81af' from origin 'https://localhost:44374' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

为什么这表现为 CORS 错误?我错过了什么?

【问题讨论】:

  • 你能提供给我们web.config文件吗?
  • 返回码是什么?
  • 本地控制台/调试日志显示什么?可能是因为 500 响应而引发异常,并且 CORS 错误是一个红鲱鱼。
  • 感谢大家的帮助。我错过了 web.config 中的 IIS 安全设置。再次感谢。

标签: c# asp.net-mvc asp.net-core asp.net-web-api


【解决方案1】:

这个错误可能是 413.13,浏览器倾向于将它们掩盖为 CORS 问题,我上个月遇到了同样的错误。

情况是:您的 .Net 应用程序已准备好接收更大的文件,但您需要告诉 IIS 接受它们,因为您将 IIS 用作您的网络服务器。

为此,在<system.webServer> 标记内的web.config 文件中添加以下行

 <security>
    <requestFiltering>
        <!-- Configures IIS to accept files up to 500MB -->
        <requestLimits maxAllowedContentLength="524288000" />
    </requestFiltering>
</security>

【讨论】:

  • 就是这样!谢谢先生。
猜你喜欢
  • 2018-10-13
  • 2020-07-26
  • 2016-01-28
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2020-03-12
  • 2019-06-29
  • 2020-10-13
相关资源
最近更新 更多