使用 FluentFTP 从 FTP 服务器流式传输文件到 ASP.NET Core 中的 Web 客户端

Stream file from FTP server using FluentFTP to web client in ASP.NET Core

我有一个共享的虚拟主机服务,我的 ASP.NET 核心应用程序运行它,还有一个 FTP 服务器。我想提供客户需要从站点下载的文件。 我不希望每个人都可以访问这些文件,所以这就是我所做的(我使用 FluentFTP):

var cred = new NetworkCredential(_config.UserName, _config.Password);
FtpClient client = new FtpClient(_config.Host, int.Parse(_config.Port), cred);
await client.ConnectAsync();
var remotePath = $"{_config.Folder}{FILE_DIR}/{filename}";
var result = await client.DownloadAsync(remotePath: remotePath, 0L);
await client.DisconnectAsync();
if (result != null)
{
     return File(result, "application/force-download", filename)
}
else
{
    throw new Exception("Failed");
}

问题是,服务器尝试从 FTP 服务器下载文件,然后将其发送给客户端。上传也有同样的问题,客户端需要上传文件到服务器,然后服务器再上传到FTP服务器。我认为这对于大文件来说可能非常耗时。

有没有更好的方法来实现这个目标?或者是否可以将正在从 FTP 下载的文件同时写入客户端响应,以便客户端下载在服务器中下载的任何文件,而不必等待下载到服务器完成才能开始下载?

提前致谢。

使用 FtpClient.Download and HttpResponse.OutputStream:

将文件直接下载到 HTTP 输出流
Response.AddHeader("Content-Disposition", $"attachment; filename={filename}");

client.Download(Response.OutputStream, remotePath);

原生 .NET 的类似问题 FtpWebRequest/WebClient:
Download file from FTP and how prompt user to save/open file in ASP.NET C#


另请注意,application/force-download 似乎是一个 hack。
使用Content-Disposition: attachment强制下载。