繁体   English   中英

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

但是如果上传之前远程文件已经存在,则会出现此错误

远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。

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

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

这是日志

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服务器返回FTP状态代码550。 .NET框架中的FTP实现将所有FTP状态代码转换为它自己的(本地化)消息。 特别地,代码550被翻译为“文件不可用” 在某些情况下(如可能这样),隐藏了真正的问题。


如果要通过在上传之前删除现有文件来破解它,请参阅C#中的从FTP删除文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM