FTP Synchronization
I'm playing with using FTP to periodically upload small data files from a program to a server. Users access the data from a web page which reads the data with the 开发者_StackOverflow社区javascript XMLHttpRequest function. It all seems to work but I'm struggling with some problems caused by the FTP and XMLHttpRequest getting in each others way. The only idea I've come up with is to retry failed uploads and detecting either failed XMLHttpRequests or those that return incomplete data and retrying those.
I'd like to use something simple like FTP since users of the application will probably not be able to host servers (they are likely behind NAT routers and have no fixed IP numbers) and not have access to any fancy external servers.
Anyone have any suggestions?
What if you avoid file locking problems by uploading the file under a temporary name and then renaming it?
pseduo code:
FTPSend "c:\readme.txt" /as "readme.txt.tmp"
if error retry FTPSend
FTPRename "readme.txt.tmp" /as "readme.txt"
if error retry rename
Of course you would also want to limit the amount of time you spend retrying a failed operation so it doesn't get hung in a loop.
Scrap FTP (it is not NAT friendly) and do your uploads over HTTP. You already have a system in place that can handle HTTP (since you are using XHR). You can accept overloads via a POST request and include some integrity checking before overwriting existing content or announcing new content.
My app is intended for use by people who don't have access to a server they can control. I.e. they are likely to use file space provided by their ISP or some free FTP site. Therefore any http post function will not work.
Therefore, I'm using FTP with the rename suggestion above.
1) delete file TEMP.htm if it exists 2) upload filr TEMP.htm 3) delete the target file, retry some number of times 4) rename TEMP.htm to target file
There some time between 3) and 4) when there is no target so web refernces to it might fail. The page that uses the files also has to retry the access.
精彩评论