Restart a download


Sometimes I try to download a large file and it times out. Here is a section of the log:

[20101220 19:11:17] 257 "/ResultDocs" is your current location
[20101220 19:12:20] SIZE
[20101220 19:12:20] 213 900186896
[20101220 19:12:31] PASV
[20101220 19:12:31] 227 Entering Passive Mode (63,232,53,150,39,85)
[20101220 19:12:31] Opening data connection to Port: 10069
[20101220 19:12:31] RETR
[20101220 19:12:31] 150-Accepted data connection
[20101220 19:12:31] 150 879088.8 kbytes to download
[20101220 21:37:45] Transfer Timeout (30s). Closing data connection.
[20101220 21:37:45] 888966400 bytes transferred. (99.6 KB/s) (02:25:13)
[20101220 21:38:15] Timeout (30s).
[20101220 21:38:15] 3
[20101220 21:38:15] Client closed the connection.
[20101220 22:42:59] SmartFTP FTP Library

My line of code that runs the download is:

ret = objFTP.DownloadFile(FileName, ResultDocPath & FileName, nRestartLo, nRestartHi)

The return value is 3

My question is, how do I try again/resume without restarting from the begining? I know it has something to do with the values nRestartLo and nRestartHi, but I can't find a peice of sample code on your web site that shows that. I'm sure this is a simple procedure. Can you please point me to a sample of this?


You need to get the file size of the remote file. Then, depending on your rules you are deciding whether you want to resume the existing file.
To get the file size use the Stat() function. In C++:

long nSFTPFlags = 0;

if(nFlags & sfTransferQueueConnectionGetFileItemSize)
nSFTPFlags |= sfFTPLib::ftpSFTPItemAttributeSize;

if(nFlags & sfTransferQueueConnectionGetFileItemModifyTime)
nSFTPFlags |= sfFTPLib::ftpSFTPItemAttributeModifyTime;

if(nFlags & sfTransferQueueConnectionGetFileItemCreateTime)
nSFTPFlags |= sfFTPLib::ftpSFTPItemAttributeCreateTime;

if(nFlags & sfTransferQueueConnectionGetFileItemAccessTime)
nSFTPFlags |= sfFTPLib::ftpSFTPItemAttributeAccessTime;

if (nFlags & sfTransferQueueConnectionGetFileItemPermissions)
nSFTPFlags |= sfFTPLib::ftpSFTPItemAttributePermissions;
// Request other attributes
nSFTPFlags |= sfFTPLib::ftpSFTPItemAttributeBits;

if (nFlags & (sfTransferQueueConnectionGetFileItemOwner | sfTransferQueueConnectionGetFileItemGroup))
nSFTPFlags |= sfFTPLib::ftpSFTPItemAttributeOwnerGroup;

sfFTPLib::enumError ret = m_pSFTPConnection->Stat(bstrPath, nSFTPFlags);
if(ret == sfFTPLib::ftpErrorSuccess)
sfFTPLib::ISFTPItemPtr pLastItem = m_pSFTPConnection->LastItem;

Thanks for your response. I'm a bit new to this, so I need a little more help.

I run the DownloadFile option to get a rather large file. It times out before it finishes.
I understand from your post that I need to get the size of the file that is on my local drive and then compare it to the size of the file on the remote site. I am using VB6, so the command to get the size of the file on the remote site is (I think):

s = objFTP.GetFileSize(FileName)

Unfortunately this returns a 15. Not nearly the size. It is actually about 800 meg

I then get the size of the file that was downloaded. In my case it usually dies around 6 or 7 hundred meg.

After I establish the fact that the entire file was not downloaded, I'm not sure what the next line of code would be. I think I run the DownloadFile command again, but this time include the the parameters nRestartLo and nRestartHi. I'm not sure what values to put in each of these parameters.

Is there a snippet of code somewhere on your site that runs through this?


After the GetFileSize returns 0, use the LastFileSize property to get the file size. GetFileSize returns the error code. 0=no error, 15=ftpErrorWrongReply.

Thank you. I now see how to get the file size.

Now that I can tell that the entire file was not downloaded, how do I resume the download?


Pass the filesize into the restart arguments:
objFTP.DownloadFile(FileName, ResultDocPath & FileName, nRestartLo, nRestartHi)