为什么我在试图 post 一个大文件到 Core Web API 时得到一个 404

Why do I get a 404 trying to post a large file to a Core Web API

我对 HttpClient 和 Web API 之间的文件传输非常陌生,所以请原谅我的代码中的任何无知和猜测。我一直在尝试 post 使用 System.IO.Compression.ZipFile 创建的文件到我的网站 API 现在大约一天了,但我总是收到 404 响应。如果我 post 使用空流,则会调用 API 操作方法,所以我知道 404 是由于内容而不是 URI。

此方法位于尝试 post 文件的客户端 WPF 应用程序中:

public async Task PostDirAsync(string localDirPath, string serverDir)
{
    var sourcePath = Path.Combine("Temp", Guid.NewGuid() + ".zip");
    ZipFile.CreateFromDirectory(localDirPath, sourcePath, CompressionLevel.Fastest, true);
    StreamContent streamContent;
    using (var fs = File.Open(sourcePath, FileMode.Open))
    {
        var outStream = new MemoryStream();
        await fs.CopyToAsync(outStream);
        outStream.Position = 0;
        streamContent = new StreamContent(outStream);
    }
    streamContent.Headers.Add("Content-Type", "application/octet-stream");
    var resp = await _client.PostAsync("api/File/PostDir?serverPath={WebUtility.UrlEncode(serverDir)}", streamContent);
}

这是 Web API 中接收 post 的操作方法,但前提是我在尝试 post 之前不执行 outStream.Position = 0; :

[HttpPost("PostDir")]
[DisableRequestSizeLimit]
public async Task<IActionResult> PostDir(string serverPath)
{           
    var zipName = Path.Combine(_config["QuickDrive:TempDir"], Guid.NewGuid() + ".zip");
    using (var ms = new MemoryStream())
    using (var fileStream = System.IO.File.Create(zipName))
    {
        await Request.Body.CopyToAsync(ms);
        ms.Position = 0;
        await ms.CopyToAsync(fileStream);
    }
    return Ok();
}

action 方法被调用并运行时没有错误,但流是空的,但是因为它写入了一个空文件而非常无用。我做错了什么?

如评论中所述,您的第一个问题是未使用 Stream.Position = 0 重置文件复制中涉及的 Stream 个实例。我知道您已经进行了这些更改,但我只想强调这是一个分为两部分的解决方案。

所以,第二部分:

在您的示例代码中,您添加了 [DisableRequestSizeLimit] 注释以绕过默认的 ASP.NET Core 2.0+ Kestrel 请求限制。但是,IIS 也有一个限制,默认情况下为 30MB。当超过此大小限制时,IIS 本身会生成 404 响应,这就是您所看到的。

This answer 解释了如何使用自定义 Web.config 更改此限制(为了完整性包含在下面):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- 1 GB -->
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

作为旁注:

除非有特殊原因,否则您可以避免在代码中使用 MemoryStream,直接将 fs 传递给 new StreamContent(...)。您可以对 Request.Body 流执行类似的操作,并将其直接复制到输出 FileStream 中。这将以:

public async Task PostDirAsync(string localDirPath, string serverDir)
{
    var sourcePath = Path.Combine("Temp", Guid.NewGuid() + ".zip");
    ZipFile.CreateFromDirectory(localDirPath, sourcePath, CompressionLevel.Fastest, true);

    var streamContent = new StreamContent(File.Open(sourcePath, FileMode.Open));
    streamContent.Headers.Add("Content-Type", "application/octet-stream");
    var resp = await _client.PostAsync("api/File/PostDir?serverPath={WebUtility.UrlEncode(serverDir)}", streamContent);
}

还有:

[HttpPost("PostDir")]
[DisableRequestSizeLimit]
public async Task<IActionResult> PostDir(string serverPath)
{           
    var zipName = Path.Combine(_config["QuickDrive:TempDir"], Guid.NewGuid() + ".zip");
    using (var fileStream = System.IO.File.Create(zipName))
        await Request.Body.CopyToAsync(fileStream );
    return Ok();
}