Practical Approach of Downloading File From FTP Path


Steps to download a file from a FTP:

  • Create a FTP web request.
  • Create an output File stream to select the destination of the file to store.
  • Get the response from the request.
  • Write the reponses to the output file stream.
  • Now the file is downloaded from the FTP path and placed in the specified path.

Used Namespaces:

using System.Net;
using
System.IO;

Step 1: Usage:

FTPDownload("C:\\", "default.aspx", "ftp://208.43.121.51/", "UserName", "Password");

Creating NetworkCredential means passing the username and password of the FTP path:

    /// <summary>

    /// NetworkCredential just to set the User and password of ftp path.

    /// </summary>

    public static NetworkCredential CreateNetworkCredentials(string ftpUserID, string ftpPassword)

    {

         return new NetworkCredential(ftpUserID, ftpPassword);

     }

Step 2: Creating a FTP web Request.
 

    /// <summary>

    ///Creates the FTP web request

    /// </summary>

    public static FtpWebRequest CreateFTPRequest(string fullFtpPath, string fileName)

    {

         return (FtpWebRequest)FtpWebRequest.Create(new Uri(fullFtpPath + "/" + fileName));

     }


Step 3: Download a file from a FTP path and store in a local folder.
 

    //Helps to download file from FTP and save it to local folder.

    public static void FTPDownload(string path, string fileName, string fullFtpPath,

            string ftpUserID, string ftpPassword)

    {

         FileStream outputStream;

 

         //Now the FTP web request completed and ready for response.

         FtpWebRequest ftpWebRequest = GetDownloadFTPWebRequest(path, fileName, fullFtpPath, ftpUserID, ftpPassword, out outputStream);

 

         //Getting response from the request.

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

         PassFileStreamDataToStream(response, outputStream);

     }

Step 4: Setting the web request and returning it.
 

     //Download

     public static FtpWebRequest GetDownloadFTPWebRequest(string path, string fileName, string fullFtpPath,

           string ftpUserID, string ftpPassword, out FileStream fs)

     {

          //Creates file stream based on the passed path.

          fs = new FileStream(path + "\\" + fileName, FileMode.Create);

 

          //Creating FTP request.

          FtpWebRequest reqFTP = CreateFTPRequest(fullFtpPath, fileName);

 

          //Setting FTP Credentials.

          reqFTP.Credentials = CreateNetworkCredentials(ftpUserID, ftpPassword);

          reqFTP.KeepAlive = false;

          

          // What mechanism you need to perform like download,upload etc in the FTP path.

          reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

           
          // Specify the data transfer type.

          reqFTP.UseBinary = true;

 

          // Notify the server about the size of the uploaded file

          reqFTP.ContentLength = fs.Length;

 

          return reqFTP;

     }

Step 5: Writes the FTP response to file stream which creates a file in the specified path.

     /// <summary>

     /// It takes the stream as bytes from response and writes the bytes to the file stream as File.

     /// </summary>

     public static void PassFileStreamDataToStream(FtpWebResponse response, FileStream outputStream)

     {

         try

         {

              Stream ftpStream = response.GetResponseStream();

              int bufferSize = 2048;

              int readCount;

              byte[] buffer = new byte[bufferSize];

               

              //reads the stream of response based on request.

              readCount = ftpStream.Read(buffer, 0, bufferSize);

 

              //Writing Stream to the Path specified in the File stream.

              while (readCount > 0)

              {

                  outputStream.Write(buffer, 0, readCount);

                  readCount = ftpStream.Read(buffer, 0, bufferSize);

              }

 

              ftpStream.Close();

              outputStream.Close();

              response.Close();

          }

          catch (Exception ex)

          {

              GetHttpResponse().Write(ex.Message + " Download Error");

          }

     }

Step 6: Creating the response to write if an error occurs.
 

     /// <summary>

     /// Get current response.

     /// </summary>

     public static HttpResponse GetHttpResponse()

     {

         return HttpContext.Current.Response;

     }


Similar Articles