【问题标题】:FTP file (.csv) download gets truncatedFTP 文件 (.csv) 下载被截断
【发布时间】:2013-07-30 18:22:10
【问题描述】:

这会使用 FTP 下载一个 .csv 文件。该文件在服务器上为 46k。当我下载时,它会被截断为 44k。我不知道为什么......当我在 Excel 中查看数据时,它被缩短了。我将缓冲区增加到 4096 但没有骰子(这可能不是问题)。

我最初抓取了以下代码并对其进行了调整:Downloading Files Using FTPWebRequest

任何想法表示赞赏!谢谢。

    private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
    {
        int bytesRead = 0;
        byte[] buffer = new byte[2048];            

        FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, false);
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        Stream reader = request.GetResponse().GetResponseStream();
        FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);

        while (true)
        {
            bytesRead = reader.Read(buffer, 0, buffer.Length);                               

            if (bytesRead == 0)
                break;

            fileStream.Write(buffer, 0, bytesRead);
        }
    }

    private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));

        //Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
        request.Proxy = null;

        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = keepAlive;

        request.Credentials = new NetworkCredential(userName, password);

        return request;
    }

【问题讨论】:

  • 总是使用记事本查看您的 CSV 文件,并将二进制设置为 false
  • 如果在 while 循环之后添加 fileStream.Close() 会发生什么?或者,如果您将缓冲区设置得如此之大,以至于整个文件都适合一次读取?
  • @rene > fileStream.Close() 工作!好极了...如果可以的话,我会接受您的回答,但我想我们不能接受 cmets ...谢谢!

标签: ftp download ftpwebrequest


【解决方案1】:

试试这个方法:

private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{ 
    int Length = 2048;
    Byte[] buffer = new Byte[Length];
    int bytesRead = responseStream.Read(buffer, 0, Length);     

    FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, false);
    request.Method = WebRequestMethods.Ftp.DownloadFile;

    Stream reader = request.GetResponse().GetResponseStream();
    FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);

    while (bytesRead > 0)
            {
        //if (bytesRead == 0)
          //  break;
                bytesRead = responseStream.Read(buffer, 0, Length);
                fileStream.Write(buffer, 0, bytesRead);
            }

         fileStream.Close();
}

【讨论】:

  • 谢谢伙计。我错过了导致截断的 fileStream.Close() ...“while”逻辑实际上没问题 ...
猜你喜欢
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2019-04-21
相关资源
最近更新 更多