2008-10-01 8 views
2

Comment télécharger un fichier sur un serveur Web à l'aide de C++ et MFC. Nous n'utilisons pas .Net. Aurais-je besoin d'ouvrir une prise et de tout faire moi-même? Si oui, où est une bonne référence à suivre?Téléchargement de fichier MFC

Répondre

5

Vous ne souhaitez pas utiliser les appels de socket direct. Il est difficile d'obtenir HTTP de cette façon.

La méthode la plus simple consiste à utiliser les API WinINet. Consultez les documents pour InternetOpen, ce sera probablement le premier appel que vous faites. Fonctions vous aurez probablement besoin:

  • InternetOpen
  • InternetConnect
  • HttpOpenRequest
  • HttpSendRequest
  • HttpQueryInfo
  • InternetCloseHandle

Vous pouvez trouver de la documentation pour tous ces sur msdn

1

WinInet comme suggéré. Gardez à l'esprit qu'il existe des classes MFC qui enveloppent ces API. Si, pour une raison ou une autre, ces API ne sont pas flexibles en fonction de vos besoins (par exemple, vous devez implémenter une connexion via un proxy incluant l'authentification), consultez WinHTTP. C'est un sur-ensemble de WinInet (pas de wrapper MFC mais pour WinHTTP).

0

Si vous avez un serveur FTP, consultez la classe CFtpConnection.

+0

J'ai besoin de plus que le fichier. J'ai aussi besoin de champs saisis par l'utilisateur. – JonDrnek

3

Voici le code que j'ai fini par utiliser. J'ai enlevé la vérification d'erreur, et d'autres trucs de notification. Cela fait un téléchargement de formulaire en plusieurs parties.

DWORD dwTotalRequestLength; 
DWORD dwChunkLength; 
DWORD dwReadLength; 
DWORD dwResponseLength; 
CHttpFile* pHTTP = NULL; 

dwChunkLength = 64 * 1024; 
void* pBuffer = malloc(dwChunkLength); 
CFile file ; 

CInternetSession session("sendFile"); 
CHttpConnection *connection = NULL; 

try { 
//Create the multi-part form data that goes before and after the actual file upload. 

CString strHTTPBoundary = _T("FFF3F395A90B452BB8BEDC878DDBD152");  
CString strPreFileData = MakePreFileData(strHTTPBoundary, file.GetFileName()); 
CString strPostFileData = MakePostFileData(strHTTPBoundary); 
CString strRequestHeaders = MakeRequestHeaders(strHTTPBoundary); 
dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + file.GetLength(); 

connection = session.GetHttpConnection("www.YOURSITE.com",NULL,INTERNET_DEFAULT_HTTP_PORT); 

pHTTP = connection->OpenRequest(CHttpConnection::HTTP_VERB_POST, _T("/YOUURL/submit_file.pl")); 
pHTTP->AddRequestHeaders(strRequestHeaders); 
pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE); 

//Write out the headers and the form variables 
pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength()); 

//upload the file. 

dwReadLength = -1; 
int length = file.GetLength(); //used to calculate percentage complete. 
while (0 != dwReadLength) 
{ 
    dwReadLength = file.Read(pBuffer, dwChunkLength); 
    if (0 != dwReadLength) 
    { 
    pHTTP->Write(pBuffer, dwReadLength); 
    } 
} 

file.Close(); 

//Finish the upload. 
pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength()); 
pHTTP->EndRequest(HSR_SYNC); 


//get the response from the server. 
LPSTR szResponse; 
CString strResponse; 
dwResponseLength = pHTTP->GetLength(); 
while (0 != dwResponseLength) 
{ 
    szResponse = (LPSTR)malloc(dwResponseLength + 1); 
    szResponse[dwResponseLength] = '\0'; 
    pHTTP->Read(szResponse, dwResponseLength); 
    strResponse += szResponse; 
    free(szResponse); 
    dwResponseLength = pHTTP->GetLength(); 
} 

AfxMessageBox(strResponse); 

//close everything up. 
pHTTP->Close(); 
connection->Close(); 
session.Close(); 

CString CHelpRequestUpload::MakeRequestHeaders(CString& strBoundary) 
{ 
CString strFormat; 
CString strData; 
strFormat = _T("Content-Type: multipart/form-data; boundary=%s\r\n"); 
strData.Format(strFormat, strBoundary); 
return strData; 
} 

CString CHelpRequestUpload::MakePreFileData(CString& strBoundary, CString& strFileName) 
{ 
CString strFormat; 
CString strData; 

strFormat = _T("--%s"); 
strFormat += _T("\r\n"); 
strFormat += _T("Content-Disposition: form-data; name=\"user\""); 
strFormat += _T("\r\n\r\n"); 
strFormat += _T("%s"); 
strFormat += _T("\r\n"); 

strFormat += _T("--%s"); 
strFormat += _T("\r\n"); 
strFormat += _T("Content-Disposition: form-data; name=\"email\""); 
strFormat += _T("\r\n\r\n"); 
strFormat += _T("%s"); 
strFormat += _T("\r\n"); 

strFormat += _T("--%s"); 
strFormat += _T("\r\n"); 
strFormat += _T("Content-Disposition: form-data; name=\"filename\"; filename=\"%s\""); 
strFormat += _T("\r\n"); 
strFormat += _T("Content-Type: audio/x-flac"); 
strFormat += _T("\r\n"); 
strFormat += _T("Content-Transfer-Encoding: binary"); 
strFormat += _T("\r\n\r\n"); 

strData.Format(strFormat, strBoundary, m_Name, strBoundary, m_Email, strBoundary, strFileName); 

return strData; 
} 

CString CHelpRequestUpload::MakePostFileData(CString& strBoundary) 
{ 

CString strFormat; 
CString strData; 

strFormat = _T("\r\n"); 
strFormat += _T("--%s"); 
strFormat += _T("\r\n"); 
strFormat += _T("Content-Disposition: form-data; name=\"submitted\""); 
strFormat += _T("\r\n\r\n"); 
strFormat += _T(""); 
strFormat += _T("\r\n"); 
strFormat += _T("--%s--"); 
strFormat += _T("\r\n"); 

strData.Format(strFormat, strBoundary, strBoundary); 

return strData; 

} 
Questions connexes