【问题标题】:.Net core api returns 404 while saving large file.Net core api在保存大文件时返回404
【发布时间】:2019-02-04 18:14:49
【问题描述】:

我有 .net 核心 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 在本地计算机上托管时它工作正常。

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

【问题讨论】:

  • 加载该文件需要多长时间?或者获得404需要多长时间?
  • @Simonare:它会立即发生。
  • @Gautam 您应该按照 MS 文档“使用流媒体上传文件”。见:docs.microsoft.com/en-us/aspnet/core/mvc/models/…
  • 这就是我现在的做法: using (var imageFile = new FileStream(fullPath, FileMode.Create)) { imageFile.Write(bytes, 0, bytes.Length); imageFile.Flush(); }

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


【解决方案1】:

请查看Unexpected Not Found error with IIS。你需要增加/设置maxAllowedContentLength

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

【讨论】:

  • 我已经有了它们:
猜你喜欢
  • 1970-01-01
  • 2017-05-06
  • 2021-06-22
  • 2023-02-15
  • 2020-08-30
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
相关资源
最近更新 更多