title: Errors coming while uploading images to FTP
body: <p>I am getting a lot of different issues in this function :</p>
  
  <pre><code>public static bool UploadToFTP(string strFileName, string strFolderName)
          {
              bool isUploaded = false;
              string strFilename = string.Empty;
              string strFtpURI = string.Empty;
              string strFtpUserId = string.Empty;
              string strFtpPassword = string.Empty;
              byte[] buffer = null;
              FileInfo oFileInfo = null;
              FileStream oFileStream = null;
              FtpWebRequest oFtpWebRequest = null;
  
              try
              {
                  strFilename = strFileName;
                  oFileInfo = new FileInfo(strFilename);
                  strFtpURI = Constants.FtpUri;
                  strFtpUserId = Constants.FtpUserID;
                  strFtpPassword = Constants.FtpPassword;
  
                  oFtpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpURI + "/" + strFolderName + "/" + oFileInfo.Name));
  
                  oFtpWebRequest.Credentials = new NetworkCredential(strFtpUserId, strFtpPassword);
                  oFtpWebRequest.Proxy = null;
                  oFtpWebRequest.KeepAlive = false;
                  oFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
                  oFtpWebRequest.UseBinary = true;
                  oFtpWebRequest.ContentLength = oFileInfo.Length;
  
                  int iBufferLength = 2084;
  
                  buffer = new byte[iBufferLength];
  
                  int iContentLength = 0;
  
                  oFileStream = oFileInfo.OpenRead();
  
                  try
                  {
                      iContentLength = oFileStream.Read(buffer, 0, iBufferLength);
  
                      using (Stream oStream = oFtpWebRequest.GetRequestStream())
                      {
                          while (iContentLength != 0)
                          {
                              oStream.Write(buffer, 0, iContentLength);
  
                              iContentLength = oFileStream.Read(buffer, 0, iBufferLength);
                          }
                          isUploaded = true;
                          FtpUpload.TotalKBFilesUploaded =  FtpUpload.TotalKBFilesUploaded + (int)(oFileInfo.Length / 1000);
                      }
                  }
                  catch (Exception ex)
                  {
  
                  }
                  finally
                  {
                      if (oFtpWebRequest != null)
                      {
                          oFtpWebRequest.Abort();
                          oFtpWebRequest = null;
                      }
  
                      if (buffer != null)
                      {
                          buffer = null;
                      }
  
                      if (oFileStream != null)
                      {
                          oFileStream.Close();
                          oFileStream.Dispose();
                      }
                  }
              }
              catch (Exception ex)
              {
              }
              finally
              {
                  oFileInfo = null;
              }
  
              return isUploaded;
          }
  </code></pre>
  
  <p>This is uploading 1000s of images to FTP, and this methos is called in multi threaded way.</p>
  
  <p>Different errors are :</p>
  
  <p><strong>=======================================================</strong> </p>
  
  <p>Message:The operation has timed out</p>
  
  <p>Error Trace: at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.GetRequestStream() </p>
  
  <p><strong>=======================================================</strong> </p>
  
  <p>Error Message:Unable to connect to the remote server</p>
  
  <p>Error Trace: at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.GetRequestStream() </p>
  
  <p><strong>=======================================================</strong> </p>
  
  <p>Error Message:The underlying connection was closed: An unexpected error occurred on a receive.</p>
  
  <p>Error Trace: at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.CommandStream.Abort(Exception e) at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) at System.Net.FtpWebRequest.GetRequestStream()</p>
  
  <p><strong>=======================================================</strong> </p>
  
  <p>Error Message:Unable to write data to the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.</p>
  
  <p>Error Trace: at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.FtpDataStream.Write(Byte[] buffer, Int32 offset, Int32 size) 
  <strong>=======================================================</strong> </p>
  
  <p>These are few errors coming from the same method which I retrieved from LOG file.</p>
  
  <p>Any idea what could be causing this? OR I need to give some more detail?</p>