0

I am trying to modify this FTP connection method to upload multiple files in a single connection.

As you can see I have got a while loop that iterates through a filename and filepath array, I have set the WebRequest to keep alive. I am not sure what I should move out of the loop to stop new connections from constantly opening up.

This is the error I am getting:

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

Thanks in advance!

Public string FTPUploadMultipleFiles(string ftpURL, string Username, string Password, string[] filePaths, string[] fileNames)
    {
        string result = "OK";
        try
        {

            int Counter = 0;
            if (filePaths.Count() == fileNames.Count())
                while (Counter <= filePaths.Count() - 1)
                {
                    FileInfo fileInf = new FileInfo(filePaths[Counter]);
                    FtpWebRequest reqFTP;
                    // Create FtpWebRequest object from the Uri provided
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURL + "/" + fileNames[Counter]));
                    reqFTP.Credentials = new NetworkCredential(Username, Password);
                    reqFTP.KeepAlive = true;

                    // Specify the command to be executed.
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

                    // Specify the data transfer type.
                    reqFTP.UsePassive = true;

                    reqFTP.UseBinary = true;
                    // Notify the server about the size of the uploaded file
                    reqFTP.ContentLength = fileInf.Length;
                    // The buffer size is set to 2kb
                    int buffLength = 204800;
                    byte[] buff = new byte[buffLength];
                    int contentLen;

                    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
                    FileStream fs = fileInf.OpenRead();
                    try
                    {
                        // Stream to which the file to be upload is written
                        Stream strm = reqFTP.GetRequestStream();
                        // Read from the file stream 2kb at a time
                        contentLen = fs.Read(buff, 0, Convert.ToInt32(buffLength));
                        // Till Stream content ends
                        while (contentLen != 0)
                        {
                            // Write Content from the file stream to the FTP Upload Stream
                            strm.Write(buff, 0, contentLen);
                            contentLen = fs.Read(buff, 0, buffLength);

                        }
                        // Close the file stream and the Request Stream
                        strm.Close();
                        fs.Close();
                        Counter++;

                    }



                    catch (Exception ex)
                    {
                        result = ex.Message;
                    }

                }
        }

        catch (Exception ex)
        {
            result = ex.Message;
        }

        return result;

    }
Tiaan
  • 698
  • 2
  • 10
  • 32
  • Your code looks good. Show us some proof that it closes the connection. E.g. [`FtpWebRequest` log](http://stackoverflow.com/q/9664650/850848). – Martin Prikryl May 15 '17 at 10:10
  • The problem I am facing is that on the server side, for one it is receiving multiple logins but receiving no files. I need it to log in only once and transfer all the files during that one login... – Tiaan May 15 '17 at 10:57
  • "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." – Tiaan May 15 '17 at 11:00
  • Show us the log! + How does "550 File unavailable" relate to this problem? Do you really mean that uploading single files works, but in a loop it errors? – Martin Prikryl May 15 '17 at 11:13
  • Yes, this exact code works outside of a loop – Tiaan May 15 '17 at 11:14
  • So show us a log for both! – Martin Prikryl May 15 '17 at 11:23

0 Answers0