Archived

This topic is now archived and is closed to further replies.

WinInet Resume FTP download

This topic is 5117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello... I have been working on a program which will find and download mshn files from FTP servers... I have all of the ftp stuff done... except for the code to allow resuming of files. Which is pretty much a must have because the average file size here is around 50megs... I am using WinInet functions due to them being the only library of function that supported multi-threading correctly, which is also a definate must have. Right now I have the following code: For some reason the InternetSetFilePointer function gets passed all of the correct information, but when I go to retrieve data, it starts from the beggening anyway... Does anyone know of how I can do this?
void Connection::DownloadPair(DLPair dlp)
{
	//FtpGetFile(conn, dlp.remote.c_str(), dlp.local.c_str(), false, NULL, FTP_TRANSFER_TYPE_BINARY, (DWORD)&dlcbd);	// start the file transfer

	HINTERNET dlfile = FtpOpenFile(conn, dlp.remote.c_str(), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, (DWORD)&dlcbd);	// open the file

        // I tried using this open function it didn''t retrieve any data at all...

	//HINTERNET dlfile = InternetOpenUrl(conn, dlp.remote.c_str(), NULL, 0, INTERNET_FLAG_PASSIVE, (DWORD)&dlcbd);	// open the file

	
	struct _stat fs;
	FixDir(&dlp.local, ''\\'');
	int res = _stat(dlp.local.c_str(), &fs);
	if(!res)
	{
		if(fs.st_size >= dlp.size)																	// if the file that is already there is just as big or bigger that the one we are trying to download, we don''t do anything here

		{
			InternetCloseHandle(dlfile);
			return;
		}
		LONG lsize = fs.st_size;
		InternetSetFilePointer(dlfile, lsize, NULL, FILE_BEGIN, NULL);								// we should start here... not at the beggining

	}
	void *buffer = new char[buffersize];
	DWORD bytesread = 0;
	FILE *local = NULL;
	if(!(local = fopen(dlp.local.c_str(), "a")))
	{
		InternetCloseHandle(dlfile);
		return;
	}

	do
	{
		InternetReadFile(dlfile, buffer, buffersize, &bytesread);
		fwrite(buffer, bytesread, 1, local);
//		fclose(local);

	} while (bytesread == buffersize);
	InternetCloseHandle(dlfile);
	fclose(local);
}
the dlp structure holds a string specifing the local and remote files that we are dealing with along with the remote filesize. If anyone could help me out some, That would be great. Surely someone has gotten the WinInet API to resume an FTP download... Thanks Dwiel

Share this post


Link to post
Share on other sites
I just used a different client to download/resume the same file, and it worked just fine. Maybe I should try doing this part manually... maybe I can send a REST xxx command right before I try downloading something. That seems like a bad solution though because it might not always work. hmm...

any more ideas...

Thanks!

Dwiel

Share this post


Link to post
Share on other sites
Incase anyone is interested or searching, I was finally able to do this by using the function FtpCommand and manually sending the "REST" command allong with the byte number I would like to resume from... ex: "REST 1024" to resume starting on the 1024th byte. You then must download the file using FtpOpenFile and InternetReadFile. The FtpCommand must come BEFORE the open file or the downloading will not work.

Also, I found that I had to download the latest internet sdk... to download it, just goto http://msdn.microsoft.com/platformsdk/

This caused me a huge headache and around 3 or 4 hours. I finally figured it out after corresponding with a helpful person named Narendra Chandel.

anyway, hopefully someone else can benifit from this being in an easy to find place

Dwiel

Share this post


Link to post
Share on other sites