首页 > 解决方案 > .Net core api在保存大文件时返回404

问题描述

我有.net core api,它将文件保存在文件服务器上。当文件大小较小(例如 10 kb、1 Mb、10 Mb)时,它可以正常工作。但是,当我尝试保存 100 mb 的文件时,它返回:

404 - 找不到文件或目录。您要查找的资源可能已被删除、名称已更改或暂时不可用。

我更新了 kestrel 选项以忽略文件大小:

 .UseKestrel(options => {
                options.Limits.MaxRequestBodySize = null;

            })

文件保存代码:

using (var imageFile = new FileStream(fullPath, FileMode.Create))
            {
                imageFile.Write(bytes, 0, bytes.Length);
                imageFile.Flush();
            }

网页配置:

<system.webServer>
<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="4294967295" />
  </requestFiltering>
</security>

但是,当托管在服务器上时,它仍然不起作用(对于大文件大小)。虽然当我使用 iis express 在本地机器上托管时它工作正常。

可能的原因是什么?或者我在这里缺少任何设置?

标签: c#.netasp.net-web-api.net-core

解决方案


请检查IIS 的 Unexpected Not Found 错误。您需要增加/设置maxAllowedContentLength

<system.webServer>
  <security>
    <requestFiltering>
      <!-- This will handle requests up to 50MB -->
      <requestLimits maxAllowedContentLength="52428800" />
    </requestFiltering>
  </security>
</system.webServer>

推荐阅读