Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.6k views
in .NET FTP by (500 points)
I am new to edtFTPnet and have just started using it. I originally had trouble with connecting (it would time out) but once I started using the IP address instead of the URL that problem went away. But I've encountered a new problem with reading a file. I am trying to upload a ZIP file to my FTP server. That means that it is a binary file. In looking through the edtFTPnet source code, I see that it is using a StreamReader to read the file (method ReadLine in the file FTPControlSocket.cs) and it uses a <CR><LF> pair to determine when to return a line. This is not really ideal for a binary file.

Anyway, here's my code to send a file to the server and handle the upload complete event:
      void SendClick(object sender, RoutedEventArgs e)
      {
         FTPConnection   ftp;
         string         zipFile;
         
         ftp = new FTPConnection();
         // set log information
         FTPConnection.LogFile = "ftp.log";
         FTPConnection.LogLevel = LogLevel.All;
         // set all of the FTP parameters
         ftp.ServerAddress = "66.96.147.103";
         ftp.UserName = "lwtest";
         ftp.Password = "********";
         ftp.ConnectMode = FTPConnectMode.ACTIVE;
         ftp.TransferType = FTPTransferType.BINARY;
         ftp.AutoLogin = true;
         ftp.Timeout = 15000;
         // connect to the server
         ftp.Connect();
         if (ftp.IsConnected) {
            ftp.Uploaded += new FTPFileTransferEventHandler(FileUploadComplete);
            for (int i = (numberPatients - 1); i >= 0; i--) {
               uploadSuccessful = false;
               zipFile = string.Format("{0}.zip", patients[i]);
               try {
                  ftp.UploadFile(zipFile, zipFile);
               } catch (Exception) {
                  // don't do anything for now, handle it in the upload
                  // complete event handler
               }
               if (uploadSuccessful) {
                  // clear the patient from the list & interface
                  patients[i] = string.Empty;
                  numberPatients--;
                  DisplayPatients();
               }
            }
         }
         ftp.Close();
      }
      
      /// <summary>
      /// Event handler for file upload complete event.
      /// </summary>
      /// <param name="sender">Object that raised the event.</param>
      /// <param name="e">File transfer event arguments.</param>
      public void FileUploadComplete(object sender, FTPFileTransferEventArgs e)
      {
         if (e.Succeeded) {
            // the upload was successful so delete the files
            File.Delete(fetalFileName);
            File.Delete(gePort.FileName);
            File.Delete(e.LocalFile);
            uploadSuccessful = true;
         } else {
            // don't erase the files for now until we figure out what to
            // do in this situation
         }
      }

Now, here's the log file for the results from running this code:
DEBUG [FTPConnection] 3 May 2011 08:20:30.984 : Set LocalDirectory='C:\Projects\IP Trials\bin\Debug'
DEBUG [FTPClient] 3 May 2011 08:20:31.003 : Connecting to 66.96.147.103:21
DEBUG [HostNameResolver] 3 May 2011 08:20:31.012 : Resolving 66.96.147.103
DEBUG [HostNameResolver] 3 May 2011 08:20:31.012 : 66.96.147.103 resolved to 66.96.147.103
INFO [BaseSocket] 3 May 2011 08:20:31.013 : Connecting to 66.96.147.103:21 with timeout 15000 ms
DEBUG [FTPControlSocket] 3 May 2011 08:20:31.166 : Setting socket timeout=15000
INFO [FTPControlSocket] 3 May 2011 08:20:31.182 : Command encoding=System.Text.SBCSCodePageEncoding
DEBUG [FTPControlSocket] 3 May 2011 08:20:31.190 : StrictReturnCodes=False
DEBUG [FTPControlSocket] 3 May 2011 08:20:31.457 : 220-
DEBUG [FTPControlSocket] 3 May 2011 08:20:31.766 : 220 Ipage FTP Server ready
DEBUG [FTPConnection] 3 May 2011 08:20:31.771 : Connected to 66.96.147.103 (instance=0)
DEBUG [FTPControlSocket] 3 May 2011 08:20:31.810 : ---> USER lwtest
DEBUG [FTPControlSocket] 3 May 2011 08:20:31.925 : 331 Password required for lwtest
DEBUG [FTPControlSocket] 3 May 2011 08:20:31.927 : ---> PASS ********
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.151 : 230 User lwtest logged in
DEBUG [FTPConnection] 3 May 2011 08:20:32.151 : Successfully logged in
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.157 : ---> FEAT
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : 211-Features:
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : MDTM
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : MFMT
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : TVFS
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : MFF modify;UNIX.group;UNIX.mode;
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.mode*;UNIX.owner*;
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : SITE MKDIR
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : SITE RMDIR
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : SITE UTIME
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : SITE SYMLINK
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : REST STREAM
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.270 : SIZE
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.576 : 211 End
DEBUG [FTPControlSocket] 3 May 2011 08:20:32.578

1 Answer

0 votes
by (500 points)
OK, it turns out that using UploadFile() was the problem. I changed my code to use a stream instead and that worked fine. Basically, my code now looks like this (I'm only including the updated part of the code):
         ftp.Connect();
         if (ftp.IsConnected) {
            ftp.Uploaded += new FTPFileTransferEventHandler(FileUploadComplete);
            for (int i = (numberPatients - 1); i >= 0; i--) {
               uploadSuccessful = false;
               zipFile = string.Format("{0}.zip", patients[i]);
               SetStatus(string.Format("Transferring file {0}", zipFile));
               stream = File.OpenRead(zipFile);
               stream.Seek(0, SeekOrigin.Begin);
               try {
                  ftp.UploadStream(stream, zipFile);
               } catch (Exception) {
                  // don't do anything for now, handle it in the upload
                  // complete event handler
               }
               if (uploadSuccessful) {
                  // clear the patient from the list & interface
                  patients[i] = string.Empty;
                  numberPatients--;
                  DisplayPatients();
               }
            }
         }
         ftp.Close();

Now I can move on to other bugs...

Categories

...