【问题标题】:Stream file from FTP server using FluentFTP to web client in ASP.NET Core使用 FluentFTP 将文件从 FTP 服务器流式传输到 ASP.NET Core 中的 Web 客户端
【发布时间】:2021-04-27 11:36:43
【问题描述】:

我有一个共享的 webhosting 服务,我的 ASP.NET Core 应用程序运行它,还有一个 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 下载的文件写入客户端响应,以便客户端下载服务器中下载的任何文件,而不必等待下载到服务器完成才能开始下载?

提前致谢。

【问题讨论】:

    标签: c# asp.net .net asp.net-core ftp


    【解决方案1】:

    使用FtpClient.DownloadHttpResponse.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强制下载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      相关资源
      最近更新 更多