2016-03-03 214 views
-1

我是新來的編碼和有麻煩試圖名爲「test.txt」的文件上傳到這裏FTP服務器是我的代碼:C++的文件上傳到FTP

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; 
} 

任何反饋意見表示歡迎。

+0

錯誤信息,會崩潰嗎?沒有任何附加信息我們什麼也做不了。 – Lasoloz

回答

0

對不起打擾我解決它這裏是代碼,如果有人感興趣:

#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; 
} 
+0

我也會使用INTERNET_FLAG_PASSIVE標誌。根據Microsoft(https://msdn.microsoft.com/en-us/library/windows/desktop/aa383770(v=vs.85).aspx)除非FTP託管在Microsoft服務器上,否則此標誌是必需的 –