您的位置:首页 > 理论基础 > 计算机网络

MFC实现下载网络图片缓存到本地

2016-01-26 16:59 603 查看
#include <afxinet.h>

新建一个基于对话框的工程,然后给确定按钮添加如下处理事件!【引入头文件afxinet.h】
void CTestDlg::OnOK()
{
const int dwBufSize = 1024;
CInternetSession * Session = new CInternetSession;
CHttpFile* pHttpFile = NULL;
CStdioFile pLocalFile;
DWORD dwlen;
try
{
LPBYTE lpBuf = new byte[dwBufSize];
// 把这个szPath置为D://test.jpg就行了
pLocalFile.Open( "D://test.jpg",
CFile::modeCreate |
CFile::modeWrite |
CFile::typeBinary );
pHttpFile =(CHttpFile*)Session->OpenURL(
"http://hiphotos.baidu.com/q252061160/pic/item/e4a5a9f010c6502bb17ec59d.jpg",
1,
INTERNET_FLAG_TRANSFER_BINARY |
INTERNET_FLAG_RELOAD |
INTERNET_FLAG_DONT_CACHE,
NULL,
0);
while(dwlen = pHttpFile-> Read(lpBuf, dwBufSize-1 ))
{
pLocalFile.Write(lpBuf,dwlen);
}
pLocalFile.Close();
pHttpFile->Close();
pHttpFile=NULL;
delete[] lpBuf;
}
catch(CInternetException eInt)
{
eInt.Delete();
}
catch(CMemoryException eMem)
{
eMem.Delete();
}
catch(CFileException eFile)
{
eFile.Delete();
}
CDialog::OnOK();
} http://blog.csdn.net/ccjjyy/article/details/6098104
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: