1

Problem: When i ftp upload only one file at a time, the files are uploaded fine, But when i use multiple Background workers to upload files to ftp server i get exception:

  • ex {"The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."} System.Exception {System.Net.WebException}

And only Some of the files get uploaded. I am pretty sure the file exits at that location, infact in another run the file it was complaining about that does not exist is downloaded but the error shifts on another file.

Code Description: In below code i am downloading a file from one ftp server and putting it on another. This code is inside a BackgroundsWorker_DoWork Method. Background Workers are being created inside a loop.

void imageDownloadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
           string[] ftpInfo = (string[])e.Argument;
            try
            {

                ///////////////////////////Downloading///////////////////////////////////////
                string uri = String.Format("ftp://{0}/{1}/images/{2}", ftpInfo[1], ftpInfo[2], ftpInfo[5]);

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.UseBinary = true;
            request.Credentials = new NetworkCredential(ftpInfo[3], ftpInfo[4]);

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

            long cl = response.ContentLength;
            int bufferSize = 4096;
            int readCount = 0;
            byte[] buffer = new byte[bufferSize];
            MemoryStream memStream = new MemoryStream();
            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                memStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }
            response.Close();
            ///////////////////////////Uploading///////////////////////////////////////
            string uri1 = String.Format("ftp://{0}/{1}/{2}", "127.0.0.1", string.Empty, ftpInfo[5]);
            FtpWebRequest request1 = (FtpWebRequest)WebRequest.Create(uri1);
            request1.Credentials = new NetworkCredential("user", "password");
            request1.KeepAlive = false;
            request1.Method = WebRequestMethods.Ftp.UploadFile;
            request1.UseBinary = true;
            request1.ContentLength = memStream.Length;
            int buffLength = 4096;
            byte[] buff = new byte[buffLength];
            int contentLen;

            // Stream to which the file to be upload is written
            Stream strm = request1.GetRequestStream();
            memStream.Seek(0, SeekOrigin.Begin);
            contentLen = memStream.Read(buff, 0, 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 = memStream.Read(buff, 0, buffLength);
            }

            // Close the file stream and the Request Stream
            strm.Close();
            ftpStream.Close();
            memStream.Close();

        }
        catch(Exception ex)
        {
            MessageBox.Show("While Downloading File " + ftpInfo[5] + " " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            e.Result = null;
            return;
        }

Related Thread

Edit: In File Zilla Server there is an option General Settings>Perfomance Settings>Number of Threads i have set that to 20 it didnt make any difference.

Community
  • 1
  • 1
PUG
  • 4,301
  • 13
  • 73
  • 115
  • Also Dispose of your Stream object strm closing is not good enough – MethodMan Dec 13 '11 at 15:00
  • Check my answer to following link, this is the right order code should be in http://stackoverflow.com/questions/8605710/get-my-application-to-be-allowed-access-through-firewall-using-c-sharp – PUG Dec 23 '11 at 15:37

2 Answers2

0

There maybe nothing wrong with your code. That error is a Permissions error.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • it was not really a permission error, but you were right nothing was wrong with my code. – PUG Dec 14 '11 at 19:36
  • cool at least you can be confident that you write good code.. sorry I was not able to provide you a correct answer.. thanks – MethodMan Dec 14 '11 at 20:41
0

Complete stab in the dark, but does the upload target server have a connection per IP limit? If so you may be falling foul of this by exceeding the concurrent connection limit from a single IP address.

Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
  • In File Zill Server there is an option `General Settings>Perfomance Settings>Number of Threads` i have set that to 20 it didnt make any difference. – PUG Dec 13 '11 at 15:09
  • I'm not familiar with File Zill(a?) server, but the number of threads that the server runs and the number of concurrent connections per client IP that allows are two difference things, assuming the File Zill even has a number of concurrent connections limit. – Myles McDonnell Dec 13 '11 at 15:36