如何使用C++和MFC將文件上傳到網絡服務器。我們沒有使用.Net。我需要打開一個套接字並自己做一切嗎?如果是這樣,那麼可以參考哪些方面?MFC文件上傳
Q
MFC文件上傳
2
A
回答
5
你不想使用直接套接字調用。這樣很難獲得HTTP。
更簡單的方法是WinINet API。查看InternetOpen的文檔,這可能是您第一次打電話。功能,你可能會需要:
- InternetOpen
- InternetConnect
- HttpOpenRequest中
- HttpSendRequest中
- HttpQueryInfo
- InternetCloseHandle
你可以找到所有這些文檔在MSDN上
0
0
您還可以使用XMLHTTP。即使你沒有發送XML。建立在WinINet之上,但使用起來更容易一些(如果你習慣於使用COM)。
請參閱MSDN:http://msdn.microsoft.com/en-us/library/ms759148.aspx
1
WinInet建議。請記住,有些MFC包裝這些API。 如果由於某些原因,這些API不能滿足您的需求(例如您需要通過代理實現包括身份驗證在內的連接),請查看WinHTTP。它是WinInet的超集(儘管WinHTTP沒有MFC封裝)。
0
如果您有一個ftp服務器,請查看CFtpConnection類。
3
這是我最終使用的代碼。我刪除了錯誤檢查和其他通知的東西。這是一個多部分表單上傳。
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;
}
相關問題
- 1. 如何將文件從MFC客戶端(CInternetSession)上傳到ASP.NET MVC
- 2. Valums文件上傳 - 多文件上傳
- 3. Rails文件上傳:上傳文件夾
- 4. 文件類型MFC
- 5. 寫入文件mfc
- 6. 清除文件上傳後在valums上傳ajax文件上傳
- 7. 使用blueimp上傳文件上傳東西blueimp文件上傳
- 8. 上傳文件
- 9. 上傳文件
- 10. 上傳文件
- 11. 上傳文件
- 12. 上傳文件
- 13. 文件上傳
- 14. 上傳文件
- 15. 上傳文件
- 16. 上傳文件
- 17. 上傳文件
- 18. 文件上傳
- 19. 上傳文件。
- 20. 上傳文件
- 21. 文件上傳
- 22. 文件上傳
- 23. 上傳文件
- 24. 文件上傳
- 25. 上傳文件
- 26. 文件上傳
- 27. 上傳文件
- 28. 上傳文件
- 29. 文件API上傳文件
- 30. 傳遞值+上傳文件
我需要更多的文件。我還需要一些用戶輸入的字段。 – JonDrnek 2008-10-02 18:20:41