Example #1
0
        public void UploadResume(string localDirectory, string localFilename, string remoteDirectory, string remoteFileName)
        {
            _abort = false;
            FileInfo fi = new FileInfo(Path.Combine(localDirectory, localFilename));
            FtpWebRequest request = null;
            long totalBytesSend = 0;

            request = WebRequest.Create(new Uri("ftp://" + _host + ":" + Port + "/" + remoteDirectory + "/" + remoteFileName)) as FtpWebRequest;
            request.Credentials = new NetworkCredential(UserName, Password);
            request.Timeout = TimeOut;
            request.UsePassive = UsePassive;
            request.KeepAlive = KeepAlive;
            try
            {
                long remoteFileSize = 0;
                if (this.FileExists(remoteDirectory, remoteFileName, out remoteFileSize))
                {
                    request.Method = WebRequestMethods.Ftp.AppendFile;
                }
                else
                {
                    WebException webException;
                    if (!this.DirectoryExits(remoteDirectory, out webException))
                    {
                        var directoryCreated = this.CreateDirectory(remoteDirectory, out webException);
                    }
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                }
                request.ContentLength = fi.Length - remoteFileSize;
                request.UsePassive = true;

                using (Stream requestStream = request.GetRequestStream())
                {
                    using (FileStream logFileStream = new FileStream(Path.Combine(localDirectory, localFilename), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        StreamReader fs = new StreamReader(logFileStream);

                        fs.BaseStream.Seek(remoteFileSize, SeekOrigin.Begin);
                        byte[] buffer = new byte[BufferSize];
                        int readBytes = 0;
                        do
                        {
                            readBytes = fs.BaseStream.Read(buffer, 0, BufferSize);
                            requestStream.Write(buffer, 0, readBytes);
                            if (UploadProgressChanged != null && !_abort)
                            {
                                UploadProgressChanged(this, new UploadProgressChangedLibArgs(fs.BaseStream.Position, fs.BaseStream.Length));
                            }
                            //System.Threading.Thread.Sleep(500);
                        } while (readBytes != 0 && !_abort);
                        //_fileStreams.Remove( fs );
                        requestStream.Close();
                        totalBytesSend = fs.BaseStream.Length;
                        fs.Close();
                        logFileStream.Close();
                        Thread.Sleep(100);
                    }
                }
                //Console.WriteLine( "Done" );
                if (UploadFileCompleted == null || _abort) return;
                var uploadFileCompleteArgs = new UploadFileCompletedEventLibArgs(totalBytesSend, TransmissionState.Success);
                UploadFileCompleted(this, uploadFileCompleteArgs);
            }
            catch (WebException webException)
            {
                if (UploadFileCompleted != null && !_abort)
                {
                    UploadFileCompleted(this, new UploadFileCompletedEventLibArgs(totalBytesSend, TransmissionState.Failed, webException));
                }
            }
            catch (Exception exp)
            {
                if (UploadFileCompleted != null && !_abort)
                {
                    UploadFileCompleted(this, new UploadFileCompletedEventLibArgs(totalBytesSend, TransmissionState.Failed, exp));
                }
            }

        }
Example #2
0
        public void Upload(string localDirectory, string localFilename, string remoteDirectory, string remoteFileName)
        {
            _abort = false;
            FtpWebRequest request = null;
            long totalBytesSend = 0;

            try
            {
                request = WebRequest.Create(new Uri("ftp://" + _host + ":" + Port + "/" + remoteDirectory + "/" + remoteFileName)) as FtpWebRequest;
                request.Credentials = new NetworkCredential(UserName, Password);
                request.UsePassive = UsePassive;
                request.Timeout = TimeOut;
                request.KeepAlive = KeepAlive;
                WebException webException;
                Debug.WriteLine("proofing directory exists");
                if (!this.DirectoryExits(remoteDirectory, out webException))
                {
                    this.CreateDirectoryRecursive(remoteDirectory, out webException);

                }
                request.Method = WebRequestMethods.Ftp.UploadFile;

                //request.ContentLength = fi.Length - remoteFileSize;

                Debug.WriteLine("starting upload");
                using (Stream requestStream = request.GetRequestStream())
                {
                    using (FileStream fs = File.Open(Path.Combine(localDirectory, localFilename), FileMode.Open))
                    {
                        fs.Seek(0, SeekOrigin.Begin);
                        byte[] buffer = new byte[BufferSize];
                        int readBytes = 0;
                        do
                        {
                            readBytes = fs.Read(buffer, 0, BufferSize);
                            requestStream.Write(buffer, 0, readBytes);
                            if (UploadProgressChanged != null && !_abort)
                            {
                                UploadProgressChanged(this, new UploadProgressChangedLibArgs(fs.Position, fs.Length));
                            }
                            //System.Threading.Thread.Sleep(500);
                        } while (readBytes != 0 && !_abort);
                        totalBytesSend = fs.Length;
                    }
                }
                if (UploadFileCompleted != null && !_abort)
                {
                    var uploadFileCompleteArgs = new UploadFileCompletedEventLibArgs(totalBytesSend, TransmissionState.Success);
                    UploadFileCompleted(this, uploadFileCompleteArgs);
                }
            }
            catch (WebException webException)
            {
                if (UploadFileCompleted != null && !_abort)
                {
                    UploadFileCompleted(this, new UploadFileCompletedEventLibArgs(totalBytesSend, TransmissionState.Failed, webException));
                }
            }
            catch (Exception exp)
            {
                var webException = exp as WebException;
                if (UploadFileCompleted != null && !_abort)
                {
                    UploadFileCompleted(this, new UploadFileCompletedEventLibArgs(totalBytesSend, TransmissionState.Failed, webException));
                }
            }
        }// method