Download a file from an FTP server

Started by
14 comments, last by .chicken 9 years, 6 months ago

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.

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).

Niko Suni

Im pretty sure I can write to c..I tried it with "\\" now, but it didn't change anything...:(

Nik is correct, see C++ FAQ Lite for more information.

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.

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.

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?

Yes, I'm sure. I can connect to it via Filezilla.

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...

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement