Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone!

This is my first attempt in implementing a procedure to upload files via FTP. Here is what I have so far:

C#
private void ftpOKButton_Click(object sender, EventArgs e)
{
     String sourcefilepath = pathToFileName; 
     String ftpurl = fullFTPHostName;
     String ftpusername = ftpUserNameTextBox.Text.Trim();
     String ftppassword = ftpPasswordTextBox.Text.Trim();

     try
     {
          string filename = Path.GetFileName(sourcefilepath);
          string ftpfullpath = ftpurl;
          FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
          ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

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

          FileStream fs = File.OpenRead(sourcefilepath);
          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)
     {
          MessageBox.Show(ex.Message);
     }
}


My question is how can I resolve the error I'm receiving: It is failing on this line:
Stream ftpstream = ftp.GetRequestStream();
with the error message of:
"The remote server returned an error: (550) File unavailable (e.g. file not found, no access).

I've verified the file is located in the path that I'm trying to access.

Thanks everyone!
Posted

I don't see where you are setting the ftp.ContentLength in order to tell the server how much data to expect.

MSDN FTP Upload Example[^] has a working example, albeit for a text file, but the process is the same.
 
Share this answer
 
Comments
joshrduncan2012 17-Jun-13 14:45pm    
I can't accept this solution. I'm still getting the same message after copying what the MSDN had in it. When I login via FileZilla, it logs in fine. :(
I used too much of the hostname, which the system didn't like. I had to go up to right before the folders began in the hostname and clip that portion off.
 
Share this answer
 
hi..
I think this will help for you..

http://stackoverflow.com/questions/10151680/upload-file-on-ftp[^]
 
Share this answer
 
Hi Josh,
Set properties of folder to be accessible for current process.(Network credential too)

Thanks,
JMD:-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900