#region "로컬로부터 파일을 업로드 하기 위한 함수 : UploadFile"
/// ////////////////////////////////////////////////////////////
/// Makeby : HJKIM@FROM30 20090828
/// FuncName: DownloadFile
/// review : This function can download the files of server.
/// apply : ftp client download function method,
/// ////////////////////////////////////////////////////////////
public bool UploadFile(string localfile, string targetfile, bool IsfileSizeChk)
{
FileInfo fi = new FileInfo(localfile);
if (fi.Exists != true)
{
string strtemp;
strtemp = string.Format("\"{0}\" File이 존재하지 않습니다. 확인 바랍니다.", localfile);
MessageBox.Show(strtemp, "File Not Exsit!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// 1. check target
string target;
if (targetfile.Contains("/"))
{
//treat as a full path
target = AdjustDir(targetfile);
}
else
target = "/" + targetfile;
string URI = "ftp://" + axSinfo.hostname + "/" + target;
// 2. perform copy
System.Net.FtpWebRequest ftp = GetRequest(URI);
// set requeset to download a file in binary mode
ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
ftp.UseBinary = true;
// Notify FTP of the expected size
ftp.ContentLength = fi.Length;
// create byte array to store : ensure at least 1 byte!
const int BufferSize = 2048;
byte[] content = new byte[BufferSize];
int dataRead;
using (FileStream fs = fi.OpenRead())
{
try
{
// open request to send
using (Stream rs = ftp.GetRequestStream())
{
do
{
dataRead = fs.Read(content, 0, BufferSize);
rs.Write(content, 0, dataRead);
} while (!(dataRead < BufferSize));
rs.Close();
}
// local PC로 download가 정상적으로 수행되었는지 file size를 Check한다.
CheckFileStatus(Convert.ToInt32(ftp.ContentLength), fi, IsfileSizeChk);
}
catch (Exception err)
{
string strErrMsg;
strErrMsg = string.Format(@"Window MSG:{0}
[서버 접근이 가능한지 확인 바랍니다.]
[INFORM: Upload file name:{1} IP:{2}, USER ID:{3}]", err.ToString(), target, axSinfo.hostname, axSinfo.userid);
MessageBox.Show(strErrMsg, "Error Information!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// ensure file closed
fs.Close();
}
}
ftp = null;
return true;
}
#endregion
//부수적인 함수들
/// ////////////////////////////////////////////////////////////
/// Makeby : HJKIM@FROM30 20090828
/// FuncName: CheckFileStatus
/// review : This function is compare to "local file" and "server file".
/// apply : etc
/// ////////////////////////////////////////////////////////////
private void CheckFileStatus(int fsize, FileInfo fi, bool IsfileSizeChk)
{
if (IsfileSizeChk == true)
{
string strTemp;
if (fi.Exists == false)
MessageBox.Show("TESTER SIDE에 파일이 존재하지 않습니다. 확인 바랍니다.", "Error Information",
MessageBoxButtons.OK, MessageBoxIcon.Error);
if (fsize != fi.Length)
{
strTemp = string.Format("파일이 정상적으로 다운로드 되지 않았습니다. filesize [{0}] : [{1}]",
fsize, fi.Length);
MessageBox.Show(strTemp, "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
/// Amend an FTP path so that it always starts with
private string AdjustDir(string path)
{
return ((path.StartsWith("/")) ? "" : "/").ToString() + path;
}
//Get the basic FtpWebRequest object with the
//common settings and security
private FtpWebRequest GetRequest(string URI)
{
//create request
FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
//Set the login details
result.Credentials = GetCredentials();
//Do not keep alive (stateless mode)
result.KeepAlive = false;
return result;
}
/// <summary>
/// Get the credentials from username/password
/// </summary>
private System.Net.ICredentials GetCredentials()
{
return new System.Net.NetworkCredential(axSinfo.userid, axSinfo.password);
}
'프로그래밍 > .Net' 카테고리의 다른 글
User Control Event 사용(delegate, event) (0) | 2014.01.17 |
---|---|
서버PC에 있는 파일 실행시키기(host execute) (0) | 2014.01.17 |
ftp protocol을 이용하여 지정된 host로부터 파일 다운로드 하기(ftp download) (0) | 2014.01.17 |
Control Attribute 컨트롤 속성 변경하기(get/set) (0) | 2014.01.17 |
외부응용프로그램 실행하기(Process.Start 메서드) (0) | 2014.01.17 |