コード例 #1
0
ファイル: CFtp2.cs プロジェクト: doctorgu/MadeIn9
        private Uri GetRemoteUri(string FolderName, string FileName)
        {
            string Url = this.Url;

            if (!string.IsNullOrEmpty(FolderName))
            {
                Url = CPath.CombineUrl(Url, FolderName.Trim('/'));
                if (!Url.EndsWith("/"))
                {
                    Url = Url + "/";
                }
            }
            if (!string.IsNullOrEmpty(FileName))
            {
                Url = CPath.CombineUrl(Url, FileName);
            }

            //C# 폴더가 C 폴더로 만들어지는 것을 방지
            Url = CWeb.EncodeUrlForWebRequest(Url);

            Uri RemoteUri = new Uri(Url);

            return(RemoteUri);
        }
コード例 #2
0
ファイル: CFtp2.cs プロジェクト: doctorgu/MadeIn9
        /// <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);
            }
        }