Skip to content

Load large files using ftp in c# code

Mai Hu Na

Mai Hu Na

Asked Mar 6, 2013, 4:22 AM EST3 answers
Share
LinkedInX
hi all,
         i am working on loading large files of size 200 mb or more sizes  to one server to another but it gives Error
              "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
       
        please help me my function to load files is
       
       FTPUpload(string FilePath, string FTPServer, string Username, string Password)
        (
              //Reading file into a byte array
                byte[] file = null;
                file = System.IO.File.ReadAllBytes(p_FilePath);


                //Request
                System.Net.FtpWebRequest req = null;
                req = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(p_FTPServer);


                //Credentials
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(p_Username, p_Password);
                req.Credentials = credentials;


                //Request Method
                req.Method = System.Net.WebRequestMethods.Ftp.UploadFile;


                //uploading file onto FTP server
                System.IO.Stream stream = null;
                stream = req.GetRequestStream();
                stream.Write(file, 0, file.Length);
                stream.Close();
        )
     please help..Thanks

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Some time we need to download, upload file form FTP server.
    For this we need to include one namespace and it is. using System.Net

    public void DownloadFile(string HostURL, string UserName, string Password, string SourceDirectory, string FileName, string LocalDirectory)
      {
      if (!File.Exists(LocalDirectory + FileName))
      {
      try
      {
      FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create(HostURL + "/" + SourceDirectory + "/" + FileName);
      requestFileDownload.Credentials = new NetworkCredential(UserName, Password);
      requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
      FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
      Stream responseStream = responseFileDownload.GetResponseStream();
      FileStream writeStream = new FileStream(LocalDirectory + FileName, FileMode.Create);
      int Length = 2048;
      Byte[] buffer = new Byte[Length];
      int bytesRead = responseStream.Read(buffer, 0, Length);
      while (bytesRead > 0)
      {
      writeStream.Write(buffer, 0, bytesRead);
      bytesRead = responseStream.Read(buffer, 0, Length);
      }
      responseStream.Close();
      writeStream.Close();
      requestFileDownload = null;
      responseFileDownload = null;
      }
      catch (Exception ex)
      {
      throw ex;
      }
      }
      }

    Here is more example for FTP operation.

    http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp
    0
  • Mai Hu Na
    0
    hi Rasadul alam thanks for reply but i require code for upload large file and not for downloading files and my code i given at starting
    0
  • 0

    .NET Framework does not support SFTP natively. We need to use a third party or open source component to do this. Some components are

    • SharpSSH is open source and a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and can be integrated into any .NET application.
    • Granados is also an SSH client library for .NET.
    • Rebex SFTP is a versatile file-transfer component for .NET languages (such as C# or VB.NET) that provides secure file system access over an SSH channel using the SFTP protocol.

    SharpSSH example with source code: http://cybarlab.blogspot.com/2013/03/download-upload-files-form-sftp-server.html

    0