简体   繁体   English

使用 WinSCP .NET 程序集防止超过 FTP 的不完整文件传输

[英]Prevent incomplete file transfer over FTP using WinSCP .NET Assembly

I am simply trying to transfer text files from one FTP server to another using a windows service .我只是想使用windows 服务将文本文件从一台FTP服务器传输到另一台服务器。 I download the required files from source FTP server and save it locally on my system and then upload the saved file to the destination server.我从源 FTP 服务器下载所需的文件并将其保存在我的系统本地,然后将保存的文件上传到目标服务器。 For downloading and uploading files I am using WinSCP.Net Assembly .我正在使用WinSCP.Net Assembly下载和上传文件。 Here is my code that I am using to transfer files to the destination server:这是我用来将文件传输到目标服务器的代码:

WinSCP.SessionOptions sessionOptions = new WinSCP.SessionOptions();
sessionOptions.Protocol = WinSCP.Protocol.Ftp;
sessionOptions.UserName = "myUsername";
sessionOptions.Password = "myPassword"
sessionOptions.PortNumber = 21;
sessionOptions.HostName = serverIPAddress;

session.Open(sessionOptions);

WinSCP.TransferOptions transferOptions = new WinSCP.TransferOptions();
transferOptions.TransferMode = WinSCP.TransferMode.Binary;
WinSCP.TransferOperationResult transferResult;
transferResult = session.PutFiles(PathToLocalFile + filename, destinationFilePath, false, transferOptions);
transferResult.Check();

It works fine and uploads file to the server, but in case a connectivity issue occurs while transferring the file, an incomplete chunk of required file is transferred to the destination server.它工作正常并将文件上传到服务器,但如果在传输文件时出现连接问题,则所需文件的不完整块将传输到目标服务器。

I have searched the WinSCP official documentation but I couldn't find anything related to this.我已经搜索了 WinSCP 官方文档,但找不到与此相关的任何内容。

Is there any way to ensure that only complete files gets transferred to the destination otherwise (in case an error occurs during transfer), the transferred chunk of file gets deleted automatically?有没有办法确保只有完整的文件被传输到目的地,否则(如果传输过程中发生错误),传输的文件块会自动删除? (Without manually deleting the incomplete file) (无需手动删除不完整的文件)

There no way to make this automatic.没有办法让它自动。

You have to code it.您必须对其进行编码。 Just check, if the transfer failed, reconnect (if needed), and delete the partially uploaded file.只需检查,如果传输失败,重新连接(如果需要),并删除部分上传的文件。

Though as already mentioned in comments, if the transfer fails, because of problems with connection, you may not be able to reconnect to delete the file.尽管如评论中所述,如果传输失败,由于连接问题,您可能无法重新连接以删除文件。

There's no magic solution.没有神奇的解决方案。 The server should be able to deal with partial files in the first place.服务器首先应该能够处理部分文件。

See also:也可以看看:

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

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