How do I know if an FTP file has completed?

We have a C # Windows service that requests a folder that is waiting to send an FTP file. To avoid using the file while it is still being written, we first try to obtain a file lock. be random when we get a file lock after creating an FTPed file, but before writing the file, so in the end we open an empty file.

Is there anyway reliability to find out if FTP is complete?

+4
source share
7 answers

How about using the folder watcher to index the contents, and if the file size does not change for 5 minutes, you can almost guarantee that the download is complete.

The timeout can be tied to the timeout of your FTP server.

http://www.codeproject.com/KB/files/MonitorFolderActivity.aspx

+3
source

You can change the file name before downloading, and then rename it after it is completed. so it will look until it is over.

+8
source

The practice I saw is transferring two files: one which is the actual file, and then the second which we can call the .done file. The ideal is that as soon as you see the .done file that you know, the first file should be executed.

Other options include viewing the file for modifications and waiting for a certain amount of time without changes. Of course, this is not complete proof.

Edit

Kyle makes a good conclusion that adding a checksum to the .done file and / or specifying the size of the first file is good protection against fringe cases.

+5
source

I am always a big fan of the .filepart protocol, so no matter what transfer protocol you use (ftp, ssh, rsync, etc.), you have the same understanding.

This is not a direct answer to your question, but instead of looking for an ftp-based solution, a more general solution may be better for you in the long run.

(. filepart: rename test.txt to test.txt.filepart, then when done, call it back in test.txt)

+4
source

I have always used a checksum file. Thus, you send a checksum file, which indicates the file size and checksum. You will find out that the file is loaded correctly when the checksum in the first file matches the actual checksum in the file system.

+3
source

The method I used in the past is a combination of some other answers here. that is, an FTP file with a different extension to the expected one (for example, FILENAME.part), and then rename it with the corresponding extension as the last upload step.

On the server, use FileSystemWatcher to search for new files with the correct extension. FSW will not see the file until it is renamed, and the rename operation will be atomic, so the file will be completed and available at the time of renaming.

Renaming or moving files, of course, depends on what you control the download process.

If you do not have any control over the downloading of files, you will depend on the use of FSW to know that the file is loading, and then keep track of its size - if it has not changed over a long period of time, it is able to assume that it is completed.

+1
source

Instead of polling, you might want to take a look at System.IO.FileSystemWatcher.

0
source

All Articles