2

i'm using the below method to upload files from local server to FTP server, here i'm creating a new connection and initiating a new session each and every file uploading and closing the same. how to achieve this in single initiated session in c#.

this is my code

public bool UploadTempFilesToFTP()
    {
        string[] fileList;
        try
        {
            ConfiguredValues conObj = new ConfiguredValues();
            conObj.PickTheValuesFromConfigFile();
            fileList = Directory.GetFiles(conObj.tempPath);
            foreach (string FileName in fileList)
            {
                FtpWebRequest upldrequest = (FtpWebRequest)FtpWebRequest.Create(conObj.tempOutboundURL + FileName);


                upldrequest.UseBinary = true;
                upldrequest.KeepAlive = false;
                upldrequest.Timeout = -1;
                upldrequest.UsePassive = true;

                upldrequest.Credentials = new NetworkCredential(conObj.user, conObj.pass);

                upldrequest.Method = WebRequestMethods.Ftp.UploadFile;

                string destinationAddress = conObj.tempPath;

                FileStream fs = File.OpenRead(destinationAddress + FileName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();
                Stream requestStr = upldrequest.GetRequestStream();
                requestStr.Write(buffer, 0, buffer.Length);
                requestStr.Close();
                requestStr.Flush();
                FtpWebResponse response = (FtpWebResponse)upldrequest.GetResponse();
                response.Close();
                File.Delete(destinationAddress + FileName);
            }
            Console.WriteLine("Uploaded Successfully to Temp folder");
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Upload failed. {0}", ex.Message);
            return false;
        }

    } 
BUSH
  • 45
  • 1
  • 1
  • 9

2 Answers2

4

it's weird that i answer an old question but i try almost everything to upload multiple files to ftp with no luck while the solution is very simple and effective, using LOOPING - foreach solved the issue for me i use the below function to Upload the files in one simple step..

public void Uploadbulkftpfiles(string[] list)
    {
        bool ife;// is folder exists
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/folder");
            request.Credentials = new NetworkCredential("Username", "Password");
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            ife = true;
        }
        catch (Exception)
        {

            ife = false;
        }

        /////////////////////////////////////////////begin of upload process
        
        if (ife)//the folder is already exists
        {
            foreach (var str in list)
            {
                try
                {
                    FtpWebRequest requestUP2 = (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/folder" + str);
                    requestUP2.Credentials = new NetworkCredential("UserName", "Password");
                    requestUP2.Method = WebRequestMethods.Ftp.UploadFile;
                    requestUP2.KeepAlive = false;
                    requestUP2.UsePassive = true;
                    using (Stream fileStream = File.OpenRead("ftp://ftpsite.com/folder" + str))
                    using (Stream ftpStream = requestUP2.GetRequestStream())
                    {
                        fileStream.CopyTo(ftpStream);
                    }
                }
                catch (Exception ex1)
                {
                    
                    MessageBox.Show(ex1.Message);
                }
                
            }
        }
        else if (!ife)
        {
            //CREATE THE FOLDER
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp:ftpsite/folder");
                request.Credentials = new NetworkCredential("UserName", "Password");
                request.Method = WebRequestMethods.Ftp.MakeDirectory;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            }
            catch (Exception excr) { MessageBox.Show(excr.Message); }
            
            

            //UPLOAD THE FILES
            foreach (var str in list)
            {
                try
                {
                    FtpWebRequest requestUP2 = (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/folder" + str);
                    requestUP2.Credentials = new NetworkCredential("UserName", "Password");
                    requestUP2.Method = WebRequestMethods.Ftp.UploadFile;
                    requestUP2.KeepAlive = false;
                    requestUP2.UsePassive = true;
                    using (Stream fileStream = File.OpenRead("ftp://ftpsite.com/folder" + str))
                    using (Stream ftpStream = requestUP2.GetRequestStream())
                    {
                        fileStream.CopyTo(ftpStream);
                    }
                }
                catch (Exception ex1)
                {
                    
                    MessageBox.Show(ex1.Message);
                }
                
            }
        }
    }
1

The ftp protocol is intended to works on request basis.

You start a request with a method (in your case UploadFile).

The only thing you can do is to KeepAlive your request to avoid connection closing

upldrequest.KeepAlive = true;

on every request you create except the last one. This will make a login only the first FTPWebRequest.

Then when you create the last FTPWebRequest, set

upldrequest.KeepAlive = false;

and it will close the connection when done.

giammin
  • 18,620
  • 8
  • 71
  • 89
  • "The ftp protocol is intended to works on request basis.", do you have a source for that? If I use an interactive FTP client, I login once, and handle all connections within that login. – Bart Friederichs Nov 06 '15 at 10:57
  • @BartFriederichs you misunderstood me: i did not mean stateless – giammin Nov 06 '15 at 14:49