GSI

C#을 이용한 FTP로 파일 업로드 하는 코드 입니다.
MSDN에 있는
              ...........
            StreamReader sourceStream = new StreamReader("testfile.txt");
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
             .............
이런 코드를 사용하니까. 인코딩에서 데이터가 제대로 전달되지 못하는 문제가 발생합니다.
그래서 아래와같이 처리 했습니다.

        private void btnFileUploadTest_Click(object sender, EventArgs e)
        {
            Upload("blue.jpg");
            Upload("크기변환_dddd.png");
            Upload("zskin.txt");

            MessageBox.Show("Upload 가 완료되었습니다.");
        }

        private void Upload(string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://192.168.0.3:8451/" + fileInf;
            FtpWebRequest reqFTP;

            UriBuilder URI = new UriBuilder(uri);
            URI.Scheme = "ftp";

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(URI.Uri);
            reqFTP.Credentials = new NetworkCredential("administrator", "0000");
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            reqFTP.UsePassive = true;

            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;

            FileStream fs = fileInf.OpenRead();

            try
            {
                Stream strm = reqFTP.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);

                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

Posted by gsi
: