Jump to content
  • Advertisement
Sign in to follow this  
.chicken

Download a file from an FTP server

This topic is 1522 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

Ok, what I thought would just be a tiny program, done in an hour or so, again gives me a bit of a headache.

All I'm trying to do is open a connection to an FTP server and get a list of files/download a file.

 

Connection seems to work fine.

LPCWSTR user, password,...
LPCWSTR folder = "\xyz";
	
FTPOpen = InternetOpen(LPCTSTR("Title"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!FTPOpen)
	return false;
std::cout << "Internet connection opened" << std::endl;

FTPConnect = InternetConnect(FTPOpen, url, 5553, user, password, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
if (!FTPConnect)
	return false;
std::cout << "Connected to FTP server" << std::endl;

if (FtpSetCurrentDirectory(FTPConnect, folder) == FALSE)
	return false;
std::cout << "Moved to folder." << std::endl;

return true;

This works fine (it also works if folder ist just "xyz" or "/xyz").

 

Now when I try to download a file or use FtpFindFirstFile(...), it always fails.

        WIN32_FIND_DATA dirInfo;
	HINTERNET hFind;
	DWORD dwError;
	BOOL retVal = FALSE;
	TCHAR szMsgBuffer[FTP_FUNCTIONS_BUFFER_SIZE];
	TCHAR szFName[FTP_FUNCTIONS_BUFFER_SIZE];

	if (FtpGetFile(FTPConnect, L"filename.zip", L"C:\filename.zip", FALSE,
		FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN | INTERNET_FLAG_RESYNCHRONIZE, 0) == FALSE) {
		dwError = GetLastError();
		if (dwError == ERROR_INTERNET_EXTENDED_ERROR) {
			DWORD code;
			DWORD size_needed = 0;
			InternetGetLastResponseInfo(&code, NULL, &size_needed);
			char *message = (char*)malloc(size_needed + 1);
			InternetGetLastResponseInfo(&code, LPWSTR(message), &size_needed);
			std::cout << "ERROR FtpGetFile(): " << message << std::endl;
			
		}
		std::cout << "Fileload not successful." << std::endl;
	}
	hFind = FtpFindFirstFile(FTPConnect, L"*", &dirInfo, INTERNET_FLAG_DONT_CACHE, 0);
	if (hFind == NULL)
	{
		dwError = GetLastError();
		if (dwError == ERROR_NO_MORE_FILES)
		{
			StringCchCopy(szMsgBuffer, FTP_FUNCTIONS_BUFFER_SIZE, TEXT("No files found at FTP location specified."));
			retVal = TRUE;
			goto DisplayDirError;
			return;
		}
		DWORD code;
		DWORD size_needed = 0;
		InternetGetLastResponseInfo(&code, NULL, &size_needed);
		char *message = (char*)malloc(size_needed + 1);
		InternetGetLastResponseInfo(&code, LPWSTR(message), &size_needed);
		std::cout << "ERROR FtpFindFirstFile(): " << message << std::endl;

		StringCchCopy(szMsgBuffer, FTP_FUNCTIONS_BUFFER_SIZE, TEXT("FtpFindFirstFile failed."));
		goto DisplayDirError;
		return;
	}

The error message I get from InternetGetLastResponse() is "2", which - according to msdn - is "FileNotFound". I'm sure the file exists though, and at least FtpFindFirstFile() shouldn't return that same error. hFind is always NULL.

 

I googled alot already, but just can't find out what I'm doing wrong. Can anyone help please?

 

Thanks so far.

Share this post


Link to post
Share on other sites
Advertisement

-Are you sure you can write to c:\ root?

-It may be forum software's fault, but a backslash should be escaped \\ in C and C++ (and in almost all languages which share the same syntax ancestry).

Share this post


Link to post
Share on other sites

Im pretty sure I can write to c.

 

I'm pretty sure you shouldn't be able to.  You should pick a different directory.

 

As you've already shown it as Windows development, writing to the root folder on a protected drive like "C:\filename.zip", should be disabled.

 

Just open a command prompt and try to create the file. Here's what I get when I try to write to a file test.txt:

cd \

echo foo > test.txt

Access is denied.

 

Permissions on the root directory are disabled by default.

If you're able to write the file, then you're running with UAC disabled on an administrative account, or running the process as administrator after UAC, or you've messed up your permissions, or you're running on a pre-2000 Windows OS.

 

Try changing the file name to somewhere you do have permissions, and that particular error should go away.

Edited by frob

Share this post


Link to post
Share on other sites

Ok, I'm embarassed I didnt know that. Unfortunately it still doesnt work. I tried the desktop folder, I tried "c:/test/", all of it doesnt work. I also changed every "\" to "/". Didnt changed anything either...still the same error...and I'm really running out of ideas here...I'm connecting via Port 5553, could that give any problems? I tried it with deactivated firewall already, doesn't make a difference, either.

 

Thanks so far.

Share this post


Link to post
Share on other sites

Are you sure whatever you are connecting to is running an FTP server on port 5553? Most of the FTP servers I have seen use the default port of 21, though this doesn't mean every FTP server will use that port. For a quick check perhaps download a simple FTP program such as filezilla and make sure you can connect using that? 

Share this post


Link to post
Share on other sites

No more ideas? :S I still couldn't get it to work...I tried using QFTP, but that doesnt come with QT5 and I couldn't get it to work...

Share this post


Link to post
Share on other sites

In all this time you've never actually mentioned what your program outputs. That would be very useful, otherwise we're just going to be guessing blindly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!