我正在尝试创建一个将文件上传到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()发布于 2018-10-24 07:31:39
一般来说,用FTP覆盖现有文件应该没有问题。
尽管您可能没有FTP服务器上的重写权限。(至少有些) FTP服务器返回FTP状态代码550,在这种情况下。.NET框架中的FTP实现将所有FTP状态代码转换为自己的(本地化)消息。特别是代码550被翻译成“文件不可用”。在某些情况下(可能就像这个例子),这掩盖了真正的问题。
如果希望在上传之前删除现有文件,请参阅Deleting file from FTP in C#。
https://stackoverflow.com/questions/52962466
复制相似问题