1

I need to transfer files on FTP from one server to other. I have used the below code.

Out of many files this is transferring only a single file partially. For ex. I have a source file of 56KB. After running the below code, the source file is reduced to 0kb and a 0KB file was transferred to the destination instead of 56 KB file size.

I built code to transfer all the files from source to destination. But its not progressing further after transferring a single 0KB file as above.

Please help me.

static void Main(string[] args)

    {


     string DISCH_DEST = System.Configuration.ConfigurationManager.AppSettings["DISCH_DEST"]; //Contains the source folder in source server
     string FTP_DISCH = System.Configuration.ConfigurationManager.AppSettings["FTP_DISCH"]; // FTP path (ftp://***********/)
     string USERNAME = System.Configuration.ConfigurationManager.AppSettings["USERNAME"];
     string PASSWORD = System.Configuration.ConfigurationManager.AppSettings["PASSWORD"];



     DirectoryInfo DISCH_Directory = new DirectoryInfo(DISCH_DEST);

     FileInfo[] DISCH_Files = DISCH_Directory.GetFiles("*.*");

     foreach (var f in DISCH_Files)   //FETCHING FILES FROM THE BULK FOLDER (IN)

                {


                    string FN = Path.GetFileName(f.FullName);
                    int bufferSize = 1024;

                    FtpWebRequest REQ = (FtpWebRequest)WebRequest.Create(new Uri(String.Format("{0}/{1}",FTP_DISCH,FN)));
                    REQ.Credentials = new NetworkCredential(USERNAME, PASSWORD);

                    REQ.Method = WebRequestMethods.Ftp.UploadFile;                    
                    Stream FTP_Stream = REQ.GetRequestStream();

                    FileStream LOCAL_FileStream = new FileStream(f.FullName, FileMode.Create);
                    byte[] bytebuffer = new byte[bufferSize];
                    int bytesSent = FTP_Stream.Read(bytebuffer, 0, bufferSize);

                    try
                    {
                        while (bytesSent != 0)
                        {
                            LOCAL_FileStream.Write(bytebuffer, 0, bytesSent);
                            bytesSent = FTP_Stream.Read(bytebuffer, 0, bytesSent);

                        }

                    }

                    catch (Exception ex) { Console.WriteLine(ex.ToString()); }

                    LOCAL_FileStream.Close();
                    FTP_Stream.Close();
                    REQ = null;


                }


            }

Out of many files this is transferring only a single file partially. For ex. I have a source file of 56KB. After running the below code, the source file is reduced to 0kb and a 0KB file was transferred to the destination instead of 56 KB file size.

I built code to transfer all the files from source to destination. But its not progressing further after transferring a single 0KB file as above.

Please help me.

METALHEAD
  • 2,734
  • 3
  • 22
  • 37

1 Answers1

1

Right now I dont see why your code would send any files.

Your code:

Stream FTP_Stream = REQ.GetRequestStream();

FileStream LOCAL_FileStream = new FileStream(f.FullName, FileMode.Create);
byte[] bytebuffer = new byte[bufferSize];
int bytesSent = FTP_Stream.Read(bytebuffer, 0, bufferSize);

You're making a new stream, and then reading from the ftp server to put in it...

If you were sending a file it wouldnt be FileMode.Create, as that makes a new file, but FileMode.Open.

You also surely would read from LOCAL_FileStream and write to FTP_STream....

BugFinder
  • 17,474
  • 4
  • 36
  • 51