1

I am trying to upload files via FTP using the following script. The file does upload to the FTP server however the file name is always called Images and has no exstenion.

Its probably something simple i have missed but if anyone know where it is going wrong that would be help.

public static string _FTPusername = "xx";
public static string _FTPPassword = "xxxxx";
public static string _FTPServerAddress = "cp.domainname.co.uk";
public static string _ftpurl = "ftp://cp.domainname.co.uk/Images"; //= "ftp://cp.domainname.co.uk/Images";

try
{
    string filename = Path.GetFileName( source );
    string ftpfullpath = ConnectionDetails._ftpurl;
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create( ftpfullpath );
    ftp.Credentials = new NetworkCredential( ConnectionDetails._FTPusername, ConnectionDetails._FTPPassword );

    ftp.KeepAlive = true;
    ftp.UseBinary = true;
    ftp.Method = WebRequestMethods.Ftp.UploadFile;

    FileStream fs = File.OpenRead( source );
    byte[] buffer = new byte[fs.Length];
    fs.Read( buffer, 0, buffer.Length );
    fs.Close();

    Stream ftpstream = ftp.GetRequestStream();
    ftpstream.Write( buffer, 0, buffer.Length );
    ftpstream.Close();
}
catch( Exception ex )
{
    throw ex;
}
Tim M.
  • 53,671
  • 14
  • 120
  • 163
Steve
  • 215
  • 7
  • 18

2 Answers2

0

Found the issue - was a simple school boy error.

public static string _ftpurl = "cp.domainname.co.uk/Images

Should have been:

public static string _ftpurl = "cp.domainname.co.uk

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
Steve
  • 215
  • 7
  • 18
  • I though that you said you could FTP manually into the Images directory? It sounds to me like you could have saved yourself some wasted effort by being a bit less sloppy. Glad you found error in the end. – Daniel Lee Apr 10 '12 at 06:19
0

The problem would seem to be these 3 lines:

string filename = Path.GetFileName( source );
string ftpfullpath = ConnectionDetails._ftpurl;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create( ftpfullpath );

You don't use the filename variable, so the path that gets passed in is

ftp://cp.domainname.co.uk/Images

Try something like this:

string ftpfullpath = ConnectionDetails._ftpurl + "/" + filename;
Daniel Lee
  • 7,709
  • 2
  • 48
  • 57
  • Thanks for the quick reply Daniel. I think you are in the right area however i now get "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." Its like its looking for the local file on the FTP server – Steve Apr 01 '12 at 21:31
  • Do you have an Images directory on your ftp server? – Daniel Lee Apr 01 '12 at 21:37
  • I am guessing that either the path is wrong or you don't have access to the directory. Are you able to ftp manually into the Images directory with an ftp client? – Daniel Lee Apr 01 '12 at 21:46
  • It's never as simple as should be to setup FTP servers. What sort of FTP server do you have? When you are logged in, what is the start directory? Try uploading to the root directory (ftp://cp.domainname.co.uk + "/" + filename ). Try turning passive mode on. To see if you have access to the Images directory, try some code like this example to list all the files: http://msdn.microsoft.com/en-us/library/ms229716.aspx – Daniel Lee Apr 03 '12 at 11:21
  • Have a look at this: http://stackoverflow.com/a/2781767/22688 So try that as well. Change "/" to "/%2f" (or maybe "/%2f/") to get an absolute path. – Daniel Lee Apr 03 '12 at 11:37