在 C# 中覆盖 FTP 服务器上已存在的文件时出现“(550) 文件不可用”

"(550) File unavailable" when overwriting a file already existing on FTP server in C#

我正在尝试创建一种将文件上传到 FTP 服务器的方法。像这样很好用

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");
}

但是如果远程文件在上传之前已经存在,我会得到这个错误

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

当我从服务器删除文件并再次上传时,它就可以正常工作了。

我想使用相同的名称(覆盖文件)。如果它存在,我是否可以从那里删除它然后上传它?

这是日志

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()

一般来说,用 FTP 覆盖现有文件应该没有问题。

尽管您可能没有 FTP 服务器的覆盖权限。 (至少一些)FTP 服务器 return FTP 状态代码 550,在这种情况下。 FTP .NET 框架中的实现将所有 FTP 状态代码转换为它自己的(本地化)消息。特别是代码 550 被翻译成 "File unavailable"。在某些情况下(可能像这个),隐藏了真正的问题。


如果您想通过在上传前删除现有文件来破解它,请参阅Deleting file from FTP in C#