HTTP프로토콜을 이용하여 파일을 업로드하는 소스입니다.
#include <afxinet.h> //요게 필요 합니다...이게 무었을 뜻하는지 는 아시것 같고요...
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CInternetSession internetSession;
CHttpConnection * httpConnection = internetSession.GetHttpConnection("op.co.kr", (INTERNET_PORT)80,NULL ,NULL);//op.co.kr은 저희 회사 주소고요..이렇게 주소를 적고
CHttpFile * httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, "/~eorsm/up_test.php");
///~eorsm/up_test.php는 파일이 업로드 되면 실행할 페이지이고요...
httpFile->AddRequestHeaders("Accept: text/*\r\nContent-Type: multipart/form-data; boundary=---------------------------7d13a23b368\r\nUser-Agent: VCApp\r\n");
////////////////////////////////////////////////////////application/octet-stream
CString sFile = _T( str1[i]);//str[1]은 전송할 파일의 경로입니다.
CString sPrefix;
sPrefix.Format(_T("\r\n-----------------------------7d13a23b368\r\nContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n"), sFile);///upfile은 서버에서 파일을 받아 줄 변수 입니다..
DWORD dwPrefix = sPrefix.GetLength();
CString sSuffix = _T("\r\n\r\n-----------------------------7d13a23b368--\r\n");
DWORD dwSuffix = sSuffix.GetLength();
CFile postFile;
postFile.Open( str1[i], CFile::modeReadorCFile::typeBinary);
postFile.SeekToEnd();
DWORD dwFileSize = postFile.GetPosition();
postFile.SeekToBegin();
DWORD dwPostDataLen = dwPrefix + dwFileSize + dwSuffix;
BYTE * szPostData, * pCurrentByte;
szPostData = pCurrentByte = new BYTE[dwPostDataLen];
memcpy((void *)szPostData, (const void *)(LPCTSTR)sPrefix, dwPrefix);
pCurrentByte += dwPrefix;
pCurrentByte += postFile.Read((void *)pCurrentByte, dwFileSize);
memcpy((void *)pCurrentByte, (const void *)(LPCTSTR)sSuffix, dwSuffix);
//////////////////////////////////////////////////////////
try
{
httpFile->SendRequest(NULL, 0, (LPVOID)szPostData, dwPostDataLen);//요게 핵심인데 서버에 파일을 올리는 작업을 하는 함수 입니다..
DWORD dwRet;
httpFile->QueryInfoStatusCode(dwRet);
TCHAR szResp[1024];
while (httpFile->ReadString(szResp, 1024))
TRACE("%s\n", szResp);
delete (LPVOID)szPostData;
}
catch (CInternetException& exc)
{
TRACE("Caught Exception (%d)\n", exc.m_dwError);
}
httpFile->Close();
delete httpFile;
delete httpConnection;
}
return 0;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
데브피아에서 퍼 왔습니다.^^