Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:02:23+00:00 2026-06-10T03:02:23+00:00

I am trying to download files from ftp server with this code: using (System.IO.FileStream

  • 0

I am trying to download files from ftp server with this code:

using (System.IO.FileStream fileStream = System.IO.File.OpenWrite(filePath))
              {

                byte[] buffer = new byte[4096];

                int bytesRead = responseStream.Read(buffer, 0, 4096);
                while (bytesRead > 0)
                {
                  fileStream.Write(buffer, 0, bytesRead);                      
                  bytesRead = responseStream.Read(buffer, 0, 4096);
                }

              }

The creation of responseStream:

System.IO.Stream responseStream = GetFileAsStream(url, username, password, false);

public static System.IO.Stream GetFileAsStream(string ftpUrl, string username, string password, bool usePassive)
{
  System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(ftpUrl);
  request.KeepAlive = false;
  request.ReadWriteTimeout = 120000;
  request.Timeout = -1;
  request.UsePassive = usePassive;

  request.Credentials = new System.Net.NetworkCredential(username, password);

  request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;

  System.IO.Stream fileResponseStream;


  System.Net.FtpWebResponse fileResponse = (System.Net.FtpWebResponse)request.GetResponse();



  fileResponseStream = fileResponse.GetResponseStream();

  return fileResponseStream;
}

It works fine with smaller files but when a file is bigger (e.g. 150mb) the process hangs. For some reason the program does not understand that it has completed the download and it still tries to read more bytes.

I prefer answers which do not include using external libraries. Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:02:25+00:00Added an answer on June 10, 2026 at 3:02 am

    I have solved my problem by introducing a request timeout- which, if reached, makes the program to throw a WebException. In that case, the program resumes the download from the place it left of. Here’s my code :

    This is inside of a method which is returning true if the file is downloaded, false- otherwise

    Digitalez.DirectoryUtil.EnsureDirectoryExists(relativePath);
    
            string filePath = System.IO.Path.Combine(relativePath, fileInfo.Name);
            long length = Digitalez.FtpUtil.GetFileLength(fileInfo.FullPath, userName, password, usePassive);
            long offset = 0;
            int retryCount = 10;
            int? readTimeout = 5 * 60 * 1000; //five minutes
    
            // if the file exists, do not download it
            if (System.IO.File.Exists(filePath))
            {
              return false;
            }
    
            while (retryCount > 0)
            {
    
              using (System.IO.Stream responseStream = Captator.Eifos.Net.FtpUtil.GetFileAsStream(fileInfo.FullPath, userName, password, usePassive, offset, requestTimeout: readTimeout != null ? readTimeout.Value : System.Threading.Timeout.Infinite))
              {
    
                using (System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Append))
                {
                  byte[] buffer = new byte[4096];
                  try
                  {
                    int bytesRead = responseStream.Read(buffer, 0, buffer.Length);
    
                    while (bytesRead > 0)
                    {
                      fileStream.Write(buffer, 0, bytesRead);
    
                      bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    }
    
                    return true;
                  }
                  catch (System.Net.WebException)
                  {
                    // Do nothing - consume this exception to force a new read of the rest of the file
                  }
                }
    
                if (System.IO.File.Exists(filePath))
                {
                  offset = new System.IO.FileInfo(filePath).Length;
                }
                else
                {
                  offset = 0;
                }
    
                retryCount--;
    
                if (offset == length)
                {
                  return true;
                }
    
              }
            }
    

    Digitalez.FtpUtil:

    public static System.IO.Stream GetFileAsStream(string ftpUrl, string username, string password, bool usePassive, long offset, int requestTimeout)
    {
      System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(ftpUrl);
    
      request.KeepAlive = false;
      request.ReadWriteTimeout = requestTimeout;
      request.Timeout = requestTimeout;
      request.ContentOffset = offset;
      request.UsePassive = usePassive;
      request.UseBinary = true;
    
      request.Credentials = new System.Net.NetworkCredential(username, password);
    
      request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
    
      System.IO.Stream fileResponseStream;
    
      System.Net.FtpWebResponse fileResponse = (System.Net.FtpWebResponse)request.GetResponse();
    
      fileResponseStream = fileResponse.GetResponseStream();
    
      return fileResponseStream;
    }
    
    public static long GetFileLength(string ftpUrl, string username, string password, bool usePassive)
    {
      System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(ftpUrl);
    
      request.KeepAlive = false;
      request.UsePassive = usePassive;
    
    
      request.Credentials = new System.Net.NetworkCredential(username, password);
      request.Method = System.Net.WebRequestMethods.Ftp.GetFileSize;
    
      System.Net.FtpWebResponse lengthResponse = (System.Net.FtpWebResponse)request.GetResponse();
      long length = lengthResponse.ContentLength;
      lengthResponse.Close();
      return length;
    
    }
    

    I haven’t tried other servers but this certainly does the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using FtpWebRequest to download files from FTP server.Am trying to test my code
I am trying to download a file from an FTP server using an NSOperation.
I am trying this code to download a file from a Windows machine using
I am trying to download files from a server using FTP protocol in java.
I'm trying to write a script that will download files from an FTP server.
i'm trying to download .zip file from bluehost server to my computer. Like this
I am trying to implement an Asynchronous file download from a web server using
I'm trying to download files in subdirectories from an ftp server with ant. The
I am trying to download a file from a FTP server with Java. It
So far i wrote a code to download a file from ftp server then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.