Exemple #1
0
 /* Upload File */
 public void Upload(string localFile, string remoteFile)
 {
     try
     {
         /* Create an FTP Request */
         FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + remoteFile);
         /* Log in to the FTP Server with the User Name and Password Provided */
         FtpRequest.Credentials = new NetworkCredential(UserName, Password);
         LogInToInterlocutor(InterlocutorAddress);
         /* When in doubt, use these options */
         FtpRequest.UseBinary  = true;
         FtpRequest.UsePassive = true;
         FtpRequest.KeepAlive  = true;
         /* Specify the Type of FTP Request */
         FtpRequest.Method = WebRequestMethods.Ftp.UploadFile;
         /* Establish Return Communication with the FTP Server */
         StartUploadData(localFile, remoteFile, InterlocutorAddress);
         FtpStream = FtpRequest.GetRequestStream();
         /* Open a File Stream to Read the File for Upload */
         FileStream localFileStream = new FileStream(localFile, FileMode.Create);
         /* Buffer for the Downloaded Data */
         byte[] byteBuffer = new byte[BufferSize];
         int    bytesSent  = localFileStream.Read(byteBuffer, 0, BufferSize);
         /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
         try
         {
             while (bytesSent != 0)
             {
                 FtpStream.Write(byteBuffer, 0, bytesSent);
                 bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize);
             }
         }
         finally
         {
             /* Resource Cleanup */
             localFileStream.Close();
             EndUploadData(localFile, remoteFile, InterlocutorAddress);
         }
     }
     finally
     {
         FtpStream.Close();
         FtpRequest = null;
     }
 }