Question : how to be able to push several files to ftp server, i receive 503 on fifth file, how to reuse ftp connection?

hello experts,

i'm trying to push a list of files to ftp.
the problème is that my uploads stop on the fifth file, i receive a 503 error. i've browse internet with the prob, some people sais to reuse the ftp connection but in my situation i dont know how to doit.

Here's my code:
i'm trying to loop around file names, but if the ftpwebrequest needs the file in order to be created how can i reuse the same if i'm pushing different files ?`


   private void PushAllFiles()
        {
            System.IO.Stream            requestStream = null;
            System.IO.FileStream        fileStream = null;
            System.Net.FtpWebResponse   uploadResponse = null;


            string _user    = _theConfig.FTPEcologDepotUser();// FTPGatewayUser();
            string _pass    = _theConfig.FTPEcologDepotPass();
            string _server  = _theConfig.FTPEcologDepotServer();
            string _repo    = _theConfig.FTPEcologDepotServerRepo();

            try
            {
                foreach (string fileName in _theFilesToPush)
                {
                    Thread.Sleep(1000);

                    // file to upload
                    string uploadUrl = "ftp://" + _server + _repo + "/" + Path.GetFileName(fileName);
                    // setting request
                    FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uploadUrl);
                    uploadRequest.Credentials   = new NetworkCredential(_user, _pass);
                    uploadRequest.Method        = WebRequestMethods.Ftp.UploadFile;
                    uploadRequest.Proxy         = null; // UploadFile is not supported through an Http proxy // so we disable the proxy for this request.

                    requestStream = uploadRequest.GetRequestStream();
                    fileStream = File.Open(fileName, FileMode.Open);

                    byte[] buffer = new byte[1024];
                    int bytesRead;

                    while (true)
                    {
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                            break;
                        requestStream.Write(buffer, 0, bytesRead);
                    }

                    // The request stream must be closed before getting
                    // the response.
                    requestStream.Close();

                    uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
                    Console.Write("\nUpload complete:" + uploadResponse);
                    Thread.Sleep(1000);
                }
            }


            catch (UriFormatException ex)
            {
                Console.Write("\nFtpUpload->Upload : Uriformat" + ex.Message);
                throw new EplanException("error uploading ftp file");
            }
            catch (IOException ex)
            {
                Console.Write("\nFtpUpload->Upload : " + ex.Message);
                throw new EplanException("error uploading ftp file");
            }
            catch (WebException ex)
            {
                Console.Write("\nFtpUpload->Upload : " + ex.Message);
                throw new EplanException("error uploading ftp file");
            }
            finally
            {
                if (uploadResponse != null)
                    uploadResponse.Close();
                if (fileStream != null)
                    fileStream.Close();
                if (requestStream != null)
                    requestStream.Close();
            }
           
        }



thank you in advance for a code which will reuse the connections.


 

Answer : how to be able to push several files to ftp server, i receive 503 on fifth file, how to reuse ftp connection?

Give this a try, see if it gives you some ideas:

@echo off
set DestinationDirectory=X:\
for /F "tokens=*" %%A in ('dir /b /s "c:\*_daily"') do (
  robocopy "%%A" %DestinationDirectory%" /S
)

~bp
Random Solutions  
 
programming4us programming4us