只问耕耘
分类: C/C++
2006-05-29 14:29:20
Sample:
char string[25];
CString StrFileName=m_lf;
CString StrUrl=m_url;
CString sheader;
long lStartPos =0;
//打开本地文件,m_lf 是文件路径,如:C:\\a.mp3
FILE *file=NULL;
file=fopen(m_lf,"ab");
if(file!=NULL)
{
//移动文件指针到文件尾
fseek(file,0,SEEK_END);
lStartPos=ftell(file);
itoa(lStartPos,string,10);
m_byte=string;
//格式化请求头,如: 从第 1000 个字节起开始下载:“Range: bytes=999 -”
sheader.Format(_T("%sRange: bytes=%d-\r\n"), szHeaders,lStartPos);
}
else
{
MessageBox("local file failed!");
return;
}
try{
CInternetSession s;
CHttpFile *hf=NULL;
//获得要下载的文件,以二进制形式传输
hf=(CHttpFile*)s.OpenURL(m_url,1,INTERNET_FLAG_TRANSFER_BINARY,sheader,-1 );
if(hf!=NULL)
{
//开始传输数据
byte *nbytes = new byte[512];
int nReadSize=0;
nReadSize=hf->Read(nbytes,512);
while( nReadSize >0)
{
fwrite(nbytes,1,nReadSize,file);
nReadSize=hf->Read(nbytes,512);
lStartPos=ftell(file);
itoa(lStartPos,string,10);
m_byte=string;
//防止阻塞
doenents();
}
fclose(file);
hf->Close();
delete hf;
}
}catch(CInternetException *p){
// hf=NULL;
p->Delete();
}
BOOL CGfDlg::doenents()
{
MSG msg;
while(::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{
if(!AfxGetApp()->PumpMessage())
{
::PostQuitMessage(0);
return false;
}
}
return true;
}