티스토리 뷰

🌈 .NET MVC

FtpWebRequest

James Wetzel 2018. 9. 27. 11:07
728x90
반응형

private void UploadFile()

    {

        //excel 생성.

                

        // Get the object used to communicate with the server.

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://000.000.000.000/up_gmkt_get_delivery_driver_list.sql");

        request.Method = WebRequestMethods.Ftp.UploadFile;


        // This example assumes the FTP site uses anonymous logon.

        request.Credentials = new NetworkCredential("your id", "your password");


        Stream ftpStream = request.GetRequestStream();

        FileStream fs = File.OpenRead(@"D:\7E\up_gmkt_get_delivery_driver_list.sql");


        // Copy the contents of the file to the request stream.

        byte[] fileContents = new byte[1024];

        double total = (double)fs.Length;

        int byteRead = 0;

        double read = 0;


        do

        {

            byteRead = fs.Read(fileContents, 0, 1024);

            ftpStream.Write(fileContents, 0, byteRead);


            read += (double)byteRead;

            double percentage = read / total * 100;

        }

        while (byteRead != 0);

        fs.Close();

        ftpStream.Close();

    }



private void DownloadFile()

    {

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://000.000.000.000/download/up_gmkt_get_delivery_driver_list.sql");

        request.Credentials = new NetworkCredential("your id", "your password");

        request.Method = WebRequestMethods.Ftp.DownloadFile;


        FtpWebRequest request1 = (FtpWebRequest)WebRequest.Create("ftp://000.000.000.000/download/up_gmkt_get_delivery_driver_list.sql");

        request1.Credentials = new NetworkCredential("your id", "your password");

        request1.Method = WebRequestMethods.Ftp.GetFileSize;

        FtpWebResponse response = (FtpWebResponse)request1.GetResponse();

        double total = response.ContentLength;

        response.Close();


        FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create("ftp://000.000.000.000/download/up_gmkt_get_delivery_driver_list.sql");

        request2.Credentials = new NetworkCredential("your id", "your password");

        request2.Method = WebRequestMethods.Ftp.GetDateTimestamp;

        FtpWebResponse response2 = (FtpWebResponse)request2.GetResponse();

        DateTime modify = response2.LastModified;

        response2.Close();


        Stream ftpStream = request.GetResponse().GetResponseStream();

        FileStream fs = new FileStream(@"D:\CJFtp\up_gmkt_get_delivery_driver_list.sql", FileMode.Create);


        byte[] fileContents = new byte[1024];        

        int byteRead = 0;

        double read = 0;


        do

        {

            byteRead = ftpStream.Read(fileContents, 0, 1024);

            fs.Write(fileContents, 0, byteRead);


            read += (double)byteRead;

            double percentage = read / total * 100;

        }

        while (byteRead != 0);

        ftpStream.Close();

        fs.Close();

    }



Can't connect to FTP: (553) File name not allowed : 오류 발생시 아래와 같이 코드를 수정

FtpWebRequest request = 


linux ftp server

(FtpWebRequest)WebRequest.Create(@"ftp://000.00.000.000//users/interface/00eai_test/qxpress/rcvdat/SR3012081920181005110130.txt");


window ftp server

(FtpWebRequest)WebRequest.Create(@"ftp://000.00.000.000/users/interface/00eai_test/qxpress/rcvdat/SR3012081920181005110130.txt");


request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = new NetworkCredential("your id", "your password");

request.UsePassive = true;

request.UseBinary = true;

request.KeepAlive = true;


Stream ftpStream = request.GetRequestStream();

ftpStream.Write(Encoding.Default.GetBytes(stringBuilder.ToString()), 0, Encoding.Default.GetBytes(stringBuilder.ToString()).Length);

if (ftpStream != null) { ftpStream.Close(); }

728x90
반응형