1

I am trying to create a method which uploads a file to a FTP server. It works fine like this

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("xxxx", "xxxx");
    client.UploadFile("ftp://127.0.0.1/demo.xml", WebRequestMethods.Ftp.UploadFile, "D:\\Test\\demo.xml");
}

but if the remote file already exists before the upload, I get this error

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

When I delete the file from the server and again upload it then it works just fine.

I want to use the same name (overwrite the file). Is there an option that I delete it from there if it exists and then upload it?

This is the log

Step into: Stepping over property 'VigoBAS.Core.Helpers.FTP.get_LocalPath'. To step into properties or operators, go to Tools->Options->Debugging and uncheck 'Step over properties and operators (Managed only)'.
System.Net Information: 0 : [2956] FtpWebRequest#44665200::.ctor(ftp://127.0.0.1/oslo-Bruker-Fnr.xml)
System.Net Information: 0 : [2956] FtpWebRequest#44665200::GetRequestStream(Method=STOR.)
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Created connection from 127.0.0.1:61008 to 127.0.0.1:21.
System.Net Information: 0 : [2956] Associating FtpWebRequest#44665200 with FtpControlStream#20222386
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Sending command [USER TestFtpUser]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [331 Password required for testftpuser]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Sending command [PASS ********]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [230 Logged on]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [202 UTF8 mode is always enabled. No need to send this command.]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Sending command [PWD]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [257 "/" is current directory.]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Sending command [TYPE I]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [200 Type set to I]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Sending command [PASV]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [227 Entering Passive Mode (127,0,0,1,221,45)]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Sending command [STOR oslo-Bruker-Fnr.xml]
System.Net Information: 0 : [2956] FtpControlStream#20222386 - Received response [550 Permission denied]
System.Net Information: 0 : [2956] FtpWebRequest#44665200::(Releasing FTP connection#20222386.)
System.Net Error: 0 : [2956] Exception in FtpWebRequest#44665200::GetRequestStream - The remote server returned an error: (550) File unavailable (e.g., file not found, no access)..
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
   at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.GetRequestStream()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
mohsinali1317
  • 4,255
  • 9
  • 46
  • 85

1 Answers1

2

In general, there should be no problem overwriting an existing file with FTP.

Though chances are that you do not have overwrite permissions on the FTP server. (At least some) FTP servers return FTP status code 550, in such case. FTP implementation in .NET framework translates all FTP status codes to its own (localized) message. Particularly code 550 is translated to "File unavailable". That, in some cases (like probably this one), hides away the real problem.


If you want to hack it by deleting the existing file before upload, see Deleting file from FTP in C#.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992