Exemplo n.º 1
0
        public string GetParentDirectory()
        {
            var parentDir = string.Empty;

            if (!string.IsNullOrEmpty(FtpUri))
            {
                var idx = FtpUri.LastIndexOf(PathSeparator);
                parentDir = FtpUri.Substring(0, idx);
            }

            return(parentDir);
        }
Exemplo n.º 2
0
        public bool UploadFile(string localPath, int retry = 0)
        {
            _lastexception = null;
            string ErrorMessageHeader = string.Format(@"Upload file <{0}>", localPath);
            bool   success            = false;

            if (string.IsNullOrEmpty(localPath) || !File.Exists(localPath))
            {
                throw new ArgumentException("The filePath name is invalid or the file is not existed for uploading");
            }
            var fileInfo        = new FileInfo(localPath);
            var currentFilename = _fileName;
            var fileName        = Path.GetFileName(localPath);
            var retrynumber     = retry <= 0 ? 0 : retry;

#if OVER_RIDE //Currently the FTPFile require user provide the file name when instanciate the class and the file name cann't be change afterwards
            //if the behavior need to be changed, the code below need to be used.
            if (fileName != currentFilename)
            {
                Initialize(FtpUri.Replace(currentFilename, fileName));
            }
#endif

            var buffers = localPath.GetFileContenBytes(chunksize: ChunkSize);
            var append  = buffers.Count > 1 ? true : false;
            if (!append && Exists)
            {
                DeleteFile();
            }

            for (int i = 0; i < retrynumber + 1 && !success; ++i)
            {
                try
                {
                    buffers.ForEach(buffer => { UploadFile(buffer, append); });
                    if (Exists && GetFileSize() == fileInfo.Length)
                    {
                        success = true;
                    }
                }
                catch (WebException webex)
                {
                    _lastexception    = webex;
                    _lastErrorMessage = string.Format(ErrorMessageFormat,
                                                      ErrorMessageHeader,
                                                      null != webex.InnerException ? webex.InnerException.Message : webex.Message,
                                                      webex.StackTrace);
                }
                catch (Exception ex)
                {
                    _lastexception    = ex;
                    _lastErrorMessage = string.Format(ErrorMessageFormat,
                                                      ErrorMessageHeader,
                                                      null != ex.InnerException ? ex.InnerException.Message : ex.Message,
                                                      ex.StackTrace);
                }
            }

            if (_isBubbleUpException && null != _lastexception)
            {
                throw _lastexception;
            }

            return(success);
        }