Warm tip: This article is reproduced from serverfault.com, please click

c#-从 FTP 下载并使用流提供结果

(c# - Download from FTP and serve result using streams)

发布于 2021-10-20 16:20:34

在我的 Asp.Net MVC 应用程序中,我想实现一种允许我下载存储在 FTP 上的文件的方法。我想竞争以下代码以使用流并避免先下载整个文件然后将它们作为 FileResult 提供

   public FileResult getBigFileFromFTP()
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp-location.com/test/bigfile.zip");

        request.Method = WebRequestMethods.Ftp.DownloadFile;
         
        request.Credentials = new NetworkCredential("ftpuser", "ftppwd");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        
        //Missing code... I would like to serve the file during FTP download


           

    }
Questioner
treep
Viewed
0
Panagiotis Kanavos 2021-10-21 16:09:29

FileResult你可以从流中创建一个。默认情况下,ASP.NET Core 在将响应发送到浏览器之前对其进行缓冲。这可能是大文件的问题。为避免这种情况,你需要禁用缓冲。

尝试使用:

Response.BufferOutput = false;
return File(responseStream);