Reputation: 171
I am new to coding and I'm having trouble uploading a text file named "test.txt" to a ftp server.
Here is my code:
void nointernet()
{
std::cout << "No internet connection." << std::endl;
}
int upload()
{
HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hInternet)
{
nointernet();
}
HINTERNET hFtpSession = InternetConnect(hInternet, "FTPHOST", INTERNET_DEFAULT_FTP_PORT, "FTPUSER", "FTPPASS", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
if (!hFtpSession)
{
InternetCloseHandle(hInternet);
nointernet();
}
FtpPutFile(hFtpSession, "D:/test.txt", "test.txt", FTP_TRANSFER_TYPE_BINARY, 0);
std::cout << "File Uploaded." << std::endl;
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
return 0;
}
int main() {
upload();
return 0;
}
Any feedback is welcomed.
Upvotes: 4
Views: 10070
Reputation: 171
I solved it. Here is the code if someone is interested:
#include <wininet.h>
#pragma comment(lib, "Wininet")
int upload()
{
HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
HINTERNET hFtpSession = InternetConnect(hInternet, "HOST", INTERNET_DEFAULT_FTP_PORT, "USER", "PASS", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
FtpPutFile(hFtpSession, "C:/test.txt", "/test.txt", FTP_TRANSFER_TYPE_BINARY, 0);
std::cout << "File Uploaded." << std::endl;
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
return 0;
}
int main() {
upload();
return 0;
}
Upvotes: 6