Exemple #1
0
        public Int64 Download(String _remoteFile, String _localFile, FTP.TransferModeType _transferModeType)
        {
            FileStream fileStream = new FileStream(_localFile, FileMode.Create);
            TcpListener listner = null;
            TcpClient client = null;
            NetworkStream networkStream = null;

            List<FTPResponseMessage> responseMessages = new List<FTPResponseMessage>();

            Byte[] buffer = new Byte[this.BlockSize];
            Int32 bytes = new Int32();
            Int64 totalBytes = new Int32();

            this.lockTcpClient();

            this.setTransferType(_transferModeType);

            if (this.ConnectionMode == FTP.ConnectionMode.ACTIVE)
            {
                listner = this.createDataListner();
                listner.Start();
            }

            else
            {
                client = this.createDataClient();
            }

            responseMessages = this.sendCommand(FTPCommand.RETRIEVE, _remoteFile);

            if (responseMessages[0].ResponseCode != FTPServerResponseCode.OpenedNewConnection
              && responseMessages[0].ResponseCode != FTPServerResponseCode.DataConnectionAlreadyOpen
               )
            {
                throw new Exception(responseMessages[0].Message);
            }

            if (this.ConnectionMode == FTP.ConnectionMode.ACTIVE)
            {
                client = listner.AcceptTcpClient();
            }

            networkStream = client.GetStream();

            do
            {
                bytes = (Int32)networkStream.Read(buffer, 0, this.BlockSize);
                totalBytes += bytes;

                fileStream.Write(buffer, 0, bytes);
            }
            while (bytes != 0);

            networkStream.Close();
            client.Close();

            if (this.ConnectionMode == FTP.ConnectionMode.ACTIVE)
            {
                listner.Stop();
            }

            FTPResponseMessage errorMessage;

            if (responseMessages.Count == 1)
            {
                responseMessages = this.readResponse();
                errorMessage = responseMessages[0];
            }

            else
            {
                errorMessage = responseMessages[1];
            }

            if (errorMessage.ResponseCode != FTPServerResponseCode.DataConnectionClosing)
            {
                throw new Exception(errorMessage.Message);
            }

            fileStream.Close();

            this.unlockTcpClient();

            return totalBytes;
        }
Exemple #2
0
        /// <summary>
        /// Set the type of transfer
        /// </summary>
        /// <param name="_type">The transfer type</param>
        /// <returns>Returns a list of FTPResponseMessages</returns>
        private List<FTPResponseMessage> setTransferType(FTP.TransferModeType _type)
        {
            // Initializatoin
            ArrayList tempMessageList = new ArrayList();
            List<FTPResponseMessage> responseMessages = new List<FTPResponseMessage>();

            // lock the tcpClient
            this.lockTcpClient();

            switch (_type)
            {
                case FTP.TransferModeType.ASCII:

                    // set transferType to ASCII
                    responseMessages = this.sendCommand(FTPCommand.REPRESENTATION_TYPE, FTP.TransferModeTypeASCII);
                    break;

                case FTP.TransferModeType.BINARY:

                    // set transferType fo BINARY
                    responseMessages = this.sendCommand(FTPCommand.REPRESENTATION_TYPE, FTP.TransferModeTypeBinary);
                    break;

                default:

                    // unlock the tcpClient
                    this.unlockTcpClient();

                    // return a empty list
                    return responseMessages;

            }

            if (responseMessages == null)
                return null;

            if (responseMessages.Count == 0)
            {
                // unlock the tcpClient
                this.unlockTcpClient();

                // return a empty list
                return responseMessages;
            }

            if (responseMessages[0].ResponseCode != FTPServerResponseCode.CommandOK)
            {
                throw new Exception((String)tempMessageList[0]);
            }

            // unlock the tcpClient
            unlockTcpClient();

            // return the list of responseMessages
            return responseMessages;
        }
Exemple #3
0
        public Int64 Download(String _remoteFile, FTP.TransferModeType _transferModeType)
        {
            String localFilePath = String.Format("{0}{1}",
                                     PropertyManager.Property_FTP.DownloadCachePath,
                                     _remoteFile
                                     );

            return this.Download(_remoteFile, localFilePath, _transferModeType);
        }
Exemple #4
0
 public Int64 Upload(String _localfile, FTP.TransferModeType _transferModeType)
 {
     return this.Upload(_localfile, Path.GetFileName(_localfile), _transferModeType);
 }