C# FTP 서버에 파일 업로드

1945 단어 C# 기반
using System.Net;

        static private string path = @"ftp://" + "10.55.11.11" + "/";    // 10.55.2.48
        static private string ftpip = "10.55.11.11";    //ftp IP 
        static private string username = "temp";   //ftp 
        static private string password = "temp"; 

        public void Upload(string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = path +fileInf.Name;
            FtpWebRequest reqFTP;

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(username, password); 
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;
            reqFTP.ContentLength = fileInf.Length;
            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 new Exception("Ftphelper Upload Error --> " + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Download("1233.docx");
            //Download("D8Ecap.zip");
            //Download("Weekly_Report2.xlsx");
            Upload(@"E:\\ftpDownload\Mysql.pptx");
        }

좋은 웹페이지 즐겨찾기