DWORD GetUrlFileLength (CString url){ DWORD filesize; TCHAR szCause[255]; CString CauseOfError; TRY{ CInternetSession session; CHttpFile *remotefile= (CHttpFile*)session.OpenURL(url,1,INTERNET_FLAG_TRANSFER_BINARY); TCHAR szContentLength[32]; DWORD dwInfoSize = 32; DWORD dwFileSize = 0; BOOL bGotFileSize = FALSE; if (remotefile->QueryInfo ( HTTP_QUERY_CONTENT_LENGTH, szContentLength, &dwInfoSize , NULL)){ bGotFileSize = TRUE; dwFileSize = (DWORD) _ttol(szContentLength); filesize = dwFileSize;// Return 값에 할당. } else{ // 에러나 나서 파일이 없을경우에 filesize = -1 ; } remotefile->Close (); session.Close (); delete remotefile; delete session; } CATCH_ALL(error){ AfxMessageBox ("서버로부터 파일 크기를 얻어오는 과정에서 에러발생"); error->GetErrorMessage(szCause,254,NULL); CauseOfError.Format("%s",szCause); AfxMessageBox (CauseOfError); } END_CATCH_ALL; return filesize; }
위는 파일 사이즈 구하는 함수
void Ca1a1Dlg::DownloadUrlFileBuffer (CString url){ DWORD dwServiceType = AFX_INET_SERVICE_HTTP; CString szServer, szObject, szInfo; INTERNET_PORT nPort; INTERNET_PROXY_INFO m_proxyinfo; CInternetSession m_SessionDownload; CHttpConnection* m_pConnection = NULL; CHttpFile* m_pHttpFile = NULL; CFile FileWrite; DWORD d_BytesRead=0; DWORD d_FileSize=0; char szHTTPBuffer[199926]; ZeroMemory(szHTTPBuffer, sizeof(szHTTPBuffer)); //start Download Routine ::AfxParseURL(url.GetBuffer(url.GetLength()), dwServiceType, szServer, szObject, nPort); try{ m_pConnection = m_SessionDownload.GetHttpConnection(szServer,INTERNET_FLAG_KEEP_CONNECTION, nPort,NULL, NULL); m_pHttpFile= m_pConnection->OpenRequest("GET",szObject,NULL,0, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION); } catch(CInternetException* m_pException){ //exception found //lots of clean up code return; } if(m_pHttpFile){ if(!FileWrite.Open("d:\\aaa.gif", CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyNone )){ //exception found //lots of clean up code return; } try{ m_pHttpFile->SendRequest(NULL); } catch(CInternetException* m_pException){ //exception found //lots of clean up code return; } m_pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, d_FileSize); m_pHttpFile->QueryInfo(HTTP_QUERY_MAX, szInfo); while(d_BytesRead = m_pHttpFile->Read((void*)szHTTPBuffer,199926)){ FileWrite.Write((void *)szHTTPBuffer,d_BytesRead); memset(szHTTPBuffer, 0, sizeof(szHTTPBuffer)); } szServer.Empty(); szObject.Empty(); m_SessionDownload.Close(); m_pConnection->Close(); m_pHttpFile->Close(); delete m_SessionDownload; delete m_pConnection; delete m_pHttpFile; FileWrite.Close(); } }
출처: http://www.iamgsi.com/entry/MFC-Url-주소에-존재-하는-이미지-다운로드download-받기
이건 파일 다운로드 하는 함수
URLdownloadToFile이랑 차이는 이건 메모리로 직접 읽어들일수 있다 그래서 별도 작업을 할 수 있지.
답글 남기기