Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sourceFileStream"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool TransferFile(Stream sourceFileStream, string fileName)
        {
            bool result = false;

            Stream targetFtpStream = null;

            try
            {
                string transferringFName = transferringFileName(fileName);

                //:::::::::::::::::::::::::::::::::::::::::::::::::::::::

                SetupFtpSession(Url + transferringFName);

                if (AllowOverwrite)
                {
                    _ftpSession.Method = WebRequestMethods.Ftp.UploadFile;
                }
                else
                {
                    _ftpSession.Method = WebRequestMethods.Ftp.AppendFile;  // NOTE!: AppendFile method is important!
                }
                targetFtpStream = _ftpSession.GetRequestStream();

                StreamCopy.Copy(_operationFileType, sourceFileStream, targetFtpStream);

                //:::::::::::::::::::::::::::::::::::::::::::::::::::::::

                closeStream(sourceFileStream);
                closeAndFlushFtpStream(targetFtpStream);

                renameTargetFileName(transferringFName, fileName);

                //:::::::::::::::::::::::::::::::::::::::::::::::::::::::

                using (FtpWebResponse response = (FtpWebResponse)_ftpSession.GetResponse())
                {
                    LogFtpResponseCode(response);
                }
                result = true;
            }
            catch (Exception e)
            {
                closeStream(sourceFileStream);
                closeAndFlushFtpStream(targetFtpStream);

                LogManager.GetLogger("FileMate").ErrorException(string.Format("Exception while transferring file;{0}{1}{2}{3}{4}",
                                                                              Environment.NewLine, e.Message, Environment.NewLine,
                                                                              _url + fileName, Environment.NewLine), e);
                throw new FCE.TransferOperation(String.Format(
                                                    "Couldn't transfer {0} to url {1} : {2}.", fileName, _url, e.Message), Name, e);
            }
            finally
            {
            }

            return(result);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sourceFileStream"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool TransferFile(Stream sourceFileStream, string fileName)
        {
            bool         result           = false;
            FileStream   targetFileStream = null;
            BinaryWriter targetFileWriter = null;
            StreamReader sourceFileReader = null;

            string transferringFName = transferringFileName(fileName);
            string targetURI         = Path.Combine(this.Url, transferringFName);

            try
            {
                if (AllowOverwrite)
                {
                    targetFileStream = File.Open(targetURI, FileMode.Create, FileAccess.Write);
                }
                else
                {
                    targetFileStream = File.Open(targetURI, FileMode.CreateNew, FileAccess.Write);
                }

/*
 *               if (AllowOverwrite)
 *                  targetFileStream = File.Open(Path.Combine(this.Url, transferringFName), FileMode.Create, FileAccess.Write);
 *              else
 *                  targetFileStream = File.Open(Path.Combine(this.Url, transferringFName), FileMode.CreateNew, FileAccess.Write);
 */

                StreamCopy.Copy(_operationFileType, sourceFileStream, targetFileStream);

                //:::::::::::::::::::::::::::::::::::::::::::::::::::::::

                closeReader(sourceFileReader);
                closeAndFlushWriter(targetFileWriter);

                closeStream(sourceFileStream);
                closeAndFlushFileStream(targetFileStream);

                renameTargetFileName(transferringFName, fileName);

                //:::::::::::::::::::::::::::::::::::::::::::::::::::::::

                result = true;
            }
            catch (Exception e)
            {
                closeReader(sourceFileReader);
                closeAndFlushWriter(targetFileWriter);

                closeStream(sourceFileStream);
                closeAndFlushFileStream(targetFileStream);

                LogManager.GetLogger("FileMate").FatalException(string.Format("Exception occured while putting file => {0} : {1}", targetURI, e.Message), e);
                // LogManager.GetLogger("FileMate").FatalException(string.Format("Exception occured while putting file => {0} : {1}", fileName, e.Message), e);
                throw new FCE.TransferOperation(String.Format(
                                                    "Couldn't transfer {0} to {1} : {2}.", fileName, Url, e.Message), Name, e);
            }
            finally
            {
            }

            return(result);
        }