MFC文件上传
时间:2020-03-06 14:59:23 来源:igfitidea点击:
如何使用c ++和MFC将文件上传到Web服务器。我们没有使用.Net。我需要打开插座自己做所有事情吗?如果是这样,哪里有很好的参考依据?
解决方案
我们不想使用直接套接字调用。这样很难获得HTTP。
更简单的方法是使用WinINet API。查看有关InternetOpen的文档,这可能是我们打的第一通电话。我们可能需要的功能:
- 互联网开放
- 互联网连接
- HttpOpenRequest
- HttpSendRequest
- HttpQueryInfo
- InternetCloseHandle
我们可以在msdn上找到所有这些的文档
我们可以使用BITS:
http://www.codeproject.com/KB/IP/bitsman.aspx
我们也可以使用XMLHTTP。即使我们不发送XML。基于WinINet构建,但是使用起来更简单(如果我们习惯于使用COM的话)。
请参阅MSDN:http://msdn.microsoft.com/en-us/library/ms759148.aspx
WinInet的建议。请记住,有包装这些API的MFC类。
如果由于某些原因这些API不能满足要求(例如,我们需要通过包含身份验证的代理来实现连接),请看一下WinHTTP。它是WinInet的超集(尽管WinHTTP没有MFC包装器)。
如果我们有ftp服务器,请签出CFtpConnection类。
这是我最终使用的代码。我删除了错误检查和其他通知内容。这会进行多部分的表单上传。
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] = '##代码##'; 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; }