0

I'm having some trouble with uploading a file to a FTP server from C#. My code works well on localhost, but on the live environment it keeps giving me a The operation has timed out. exception.

I use the following code:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath + "/orders.csv");
request.UsePassive = true;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = -1;
StreamReader sourceStream = new StreamReader(context.Server.MapPath("~/App_Data/orders.csv"));
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

Where ftpPath is the URL of my FTP server: ftp://myserver.com

Does anyone know what I'm doing wrong here? :-)

Thanks in advance.

bomortensen
  • 3,346
  • 10
  • 53
  • 74
  • It's not clear to me if you are hosting your own server but if so, what FTP server are you using? If not, what do you mean that your code "works well on localhost"? – Kirk Woll Jun 10 '13 at 00:53
  • Hi Kirk, the FTP server can be any server specified by the user :-) in my test case, I'm using a remote ftp (my webhotel) to test. When I run my ASP.net site locally it works, but when I upload it to the (online) staging environment it gives me the timeout – bomortensen Jun 10 '13 at 00:56
  • Then what do you mean that your request "works well on localhost"? Are you trying an FTP server that is installed locally and comparing that against an FTP server installed on some remote site? – Kirk Woll Jun 10 '13 at 00:59
  • Updated my comment above ;-) – bomortensen Jun 10 '13 at 01:01
  • what will happen if you remove "request.Timeout = -1;" ? – Mohammad abumazen Jun 10 '13 at 08:02
  • Hi Mohammad, I already set it to -1 :-) It's the exact same error.. – bomortensen Jun 10 '13 at 08:40

2 Answers2

0

This was a permission issue. Seems like the FTP user on my webhotel was restricted (somehow) Tried with another FTP with a user with full permissions and it worked.

bomortensen
  • 3,346
  • 10
  • 53
  • 74
0

Some time we need to download, upload file from FTP server. Here is some example to FTP operation. For this we need to include one namespace and it is. using System.Net

public void DownloadFile(string HostURL, string UserName, string Password, string SourceDirectory, string FileName, string LocalDirectory)
        {
            if (!File.Exists(LocalDirectory + FileName))
            {
                try
                {
                    FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create(HostURL + "/" + SourceDirectory + "/" + FileName);
                    requestFileDownload.Credentials = new NetworkCredential(UserName, Password);
                    requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
                    FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
                    Stream responseStream = responseFileDownload.GetResponseStream();
                    FileStream writeStream = new FileStream(LocalDirectory + FileName, FileMode.Create);
                    int Length = 2048;
                    Byte[] buffer = new Byte[Length];
                    int bytesRead = responseStream.Read(buffer, 0, Length);
                    while (bytesRead > 0)
                    {
                        writeStream.Write(buffer, 0, bytesRead);
                        bytesRead = responseStream.Read(buffer, 0, Length);
                    }
                    responseStream.Close();
                    writeStream.Close();
                    requestFileDownload = null;
                    responseFileDownload = null;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880