天天看點

使用MFC實作檔案的上傳和下載下傳

轉載位址:http://www.cppblog.com/zgysx/archive/2006/09/28/13088.html

1、下載下傳檔案

Download(const CString& strFileURLInServer, //待下載下傳檔案的URL

const CString & strFileLocalFullPath)//存放到本地的路徑

{

 ASSERT(strFileURLInServer != "");

 ASSERT(strFileLocalFullPath != "");

 CInternetSession session;

 CHttpConnection* pHttpConnection = NULL;

 CHttpFile* pHttpFile = NULL;

 CString strServer, strObject;

 INTERNET_PORT wPort;

 DWORD dwType;

 const int nTimeOut = 2000;

 session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重試之間的等待延時

 session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);   //重試次數

 char* pszBuffer = NULL;  

 try

 {

  AfxParseURL(strFileURLInServer, dwType, strServer, strObject, wPort);

  pHttpConnection = session.GetHttpConnection(strServer, wPort);

  pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);

  if(pHttpFile->SendRequest() == FALSE)

   return false;

  DWORD dwStateCode;

  pHttpFile->QueryInfoStatusCode(dwStateCode);

  if(dwStateCode == HTTP_STATUS_OK)

  {

    HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,

         FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,

         NULL);  //建立本地檔案

   if(hFile == INVALID_HANDLE_VALUE)

   {

    pHttpFile->Close();

    pHttpConnection->Close();

    session.Close();

    return false;

   }

   char szInfoBuffer[1000];  //傳回消息

   DWORD dwFileSize = 0;   //檔案長度

   DWORD dwInfoBufferSize = sizeof(szInfoBuffer);

   BOOL bResult = FALSE;

   bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,

           (void*)szInfoBuffer,&dwInfoBufferSize,NULL);

   dwFileSize = atoi(szInfoBuffer);

   const int BUFFER_LENGTH = 1024 * 10;

   pszBuffer = new char[BUFFER_LENGTH];  //讀取檔案的緩沖

   DWORD dwWrite, dwTotalWrite;

   dwWrite = dwTotalWrite = 0;

   UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //讀取伺服器上資料

   while(nRead > 0)

   {

    WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL);  //寫到本地檔案

    dwTotalWrite += dwWrite;

    nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);

   }

   delete[]pszBuffer;

   pszBuffer = NULL;

   CloseHandle(hFile);

  }

  else

  {

   delete[]pszBuffer;

   pszBuffer = NULL;

   if(pHttpFile != NULL)

   {

    pHttpFile->Close();

    delete pHttpFile;

    pHttpFile = NULL;

   }

   if(pHttpConnection != NULL)

   {

    pHttpConnection->Close();

    delete pHttpConnection;

    pHttpConnection = NULL;

   }

   session.Close();

    return false;

  }

 }

 catch(...)

 {

  delete[]pszBuffer;

  pszBuffer = NULL;

  if(pHttpFile != NULL)

  {

   pHttpFile->Close();

   delete pHttpFile;

   pHttpFile = NULL;

  }

  if(pHttpConnection != NULL)

  {

   pHttpConnection->Close();

   delete pHttpConnection;

   pHttpConnection = NULL;

  }

  session.Close();

  return false;

 }

 if(pHttpFile != NULL)

  pHttpFile->Close();

 if(pHttpConnection != NULL)

  pHttpConnection->Close();

 session.Close();

 return true;

}

2、上傳檔案

UploadFile(LPCTSTR strURL, //負責接收上傳操作的頁面的URL

LPCTSTR strLocalFileName)  //待上傳的本地檔案路徑

{

 ASSERT(strURL != NULL && strLocalFileName != NULL);

 BOOL bResult = FALSE;

 DWORD dwType = 0;

 CString strServer;

 CString strObject;

 INTERNET_PORT wPort = 0;

 DWORD dwFileLength = 0;

 char * pFileBuff = NULL;

 CHttpConnection * pHC = NULL;

 CHttpFile * pHF = NULL;

 CInternetSession cis;

 bResult =  AfxParseURL(strURL, dwType, strServer, strObject, wPort);

 if(!bResult)

  return FALSE;

 CFile file;

 try

 {

  if(!file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead))

   return FALSE;

  dwFileLength = file.GetLength();

  if(dwFileLength <= 0)

   return FALSE;

  pFileBuff = new char[dwFileLength];

  memset(pFileBuff, 0, sizeof(char) * dwFileLength);

  file.Read(pFileBuff, dwFileLength);

  const int nTimeOut = 5000;

  cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //聯接逾時設定

  cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);  //重試1次

  pHC = cis.GetHttpConnection(strServer, wPort);  //取得一個Http聯接

  pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);

  if(!pHF->SendRequest(NULL, 0, pFileBuff, dwFileLength))

  {

   delete[]pFileBuff;

   pFileBuff = NULL;

   pHF->Close();

   pHC->Close();

   cis.Close();

   return FALSE;

  }

  DWORD dwStateCode = 0;

  pHF->QueryInfoStatusCode(dwStateCode);

  if(dwStateCode == HTTP_STATUS_OK)

   bResult = TRUE;

 }

 catch(CInternetException * pEx)

 {

  char sz[256] = "";

  pEx->GetErrorMessage(sz, 25);

  CString str;

  str.Format("InternetException occur!\r\n%s", sz);

  AfxMessageBox(str);

 }

 catch(CFileException& fe)

 {

  CString str;

  str.Format("FileException occur!\r\n%d", fe.m_lOsError);

  AfxMessageBox(str);

 }

 catch(...)

 {

  DWORD dwError = GetLastError();

  CString str;

  str.Format("Unknow Exception occur!\r\n%d", dwError);

  AfxMessageBox(str);

 }

 delete[]pFileBuff;

 pFileBuff = NULL;

 file.Close();

 pHF->Close();

 pHC->Close();

 cis.Close();

 return bResult;

}

繼續閱讀