private void ftp_BeforeFileUpload(object sender, CBeforeFileUploadEventArgs e) { FileInfo fiSrc = new FileInfo(e.LocalFullPath); //FTP에 있는 파일은 원래 파일 날짜가 아닌 업로드된 파일의 날짜 정보를 가지므로 //날짜를 비교할 수 없으므로 원본파일의 날짜만 기준이 되므로 null로 설정함. FileInfo fiDest = null; string FullPathSrcNewIs; if (!IsValidForSync(fiSrc, fiDest, true, out FullPathSrcNewIs)) { e.Cancel = true; } if (!string.IsNullOrEmpty(FullPathSrcNewIs)) { e.LocalFullPathNew = FullPathSrcNewIs; } }
/// <summary> /// FTP 서버에 클라이언트 컴퓨터의 파일을 업로드함. /// </summary> /// <param name="LocalFullPath">클라이언트 컴퓨터의 파일 전체 경로</param> /// <param name="RemoteFolder">서버의 폴더 위치</param> /// <param name="RemoteFile">서버의 파일 이름</param> public void UploadFile(string LocalFullPath, string RemoteFolder, string RemoteFile) { string LocalFullPathNew = ""; if (this.BeforeFileUpload != null) { CBeforeFileUploadEventArgs e = new CBeforeFileUploadEventArgs() { LocalFullPath = LocalFullPath }; this.BeforeFileUpload(this, e); if (e.Cancel) { return; } if (!string.IsNullOrEmpty(e.LocalFullPathNew)) { LocalFullPathNew = e.LocalFullPathNew; } } try { Uri RemoteUri = GetRemoteUri(RemoteFolder, RemoteFile); FtpWebRequest FtpReq = (FtpWebRequest)WebRequest.Create(RemoteUri); if (!string.IsNullOrEmpty(this._Info.UserId)) { FtpReq.Credentials = new NetworkCredential(this._Info.UserId, this._Info.Password); } FtpReq.UsePassive = this._Info.UsePassive; FtpReq.Method = WebRequestMethods.Ftp.UploadFile; using (FileStream readStream = new FileStream((!string.IsNullOrEmpty(LocalFullPathNew) ? LocalFullPathNew : LocalFullPath), FileMode.Open, FileAccess.Read)) { using (Stream writeStream = FtpReq.GetRequestStream()) { int Length = 256; Byte[] buffer = new Byte[Length]; int bytesRead = readStream.Read(buffer, 0, Length); while (bytesRead > 0) { writeStream.Write(buffer, 0, bytesRead); bytesRead = readStream.Read(buffer, 0, Length); } } } } catch (Exception ex) { if (this.FileUploadFailed != null) { CFileUploadFailedEventArgs e = new CFileUploadFailedEventArgs() { LocalFullPath = LocalFullPath, LocalFullPathNew = LocalFullPathNew, RemoteFullUrl = CPath.CombineUrl(RemoteFolder, RemoteFile), ex = ex }; this.FileUploadFailed(this, e); return; } else { throw new Exception(ex.Message, ex); } } if (this.AfterFileUpload != null) { CAfterFileUploadEventArgs e = new CAfterFileUploadEventArgs() { LocalFullPath = LocalFullPath, LocalFullPathNew = LocalFullPathNew, RemoteFullUrl = CPath.CombineUrl(RemoteFolder, RemoteFile) }; this.AfterFileUpload(this, e); } }