void CDownLoad::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 CDialogEx::OnOK(); UpdateData(TRUE); CString sPath = m_savePath;//下载文件的保存地址 CString m_theUrl = m_ipPath;//下载的网址 CString filename = sPath + (_T("11.mdb"));//下载文件的保存名 CInternetSession session; CString url = m_theUrl; CInternetFile* file = (CInternetFile*)session.OpenURL(url, 1, INTERNET_FLAG_TRANSFER_BINARY);//访问地址 if (file == NULL) { file->Close(); delete file; session.Close(); MessageBox(_T("下载文件失败! 错误代码:1"), _T("提示"), MB_ICONERROR); return; } CString name = filename; CFile file1; //打开保存的文件,如果该文件存在则执行重写,不存在则创建新的文件 if (file1.Open(name, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, NULL)) { //保存文件内容 int readlen = -1; char buf[1024]; while (1) { //char* buf = new char[1024]; readlen = file->Read(buf, 1024); if (readlen == 0) { //delete buf; break; } file1.Write(buf, readlen); //delete buf; ZeroMemory(buf, 1024); } file1.Close(); } else { MessageBox(_T("下载文件失败! 错误代码:2"), _T("提示"), MB_ICONERROR); file->Close(); delete file; session.Close(); return; } //完成下载后关闭文件和链接 file->Close(); delete file; session.Close(); }
此内容为自己所找资料按照项目要求做了更改后的方法,可行!
时间: 2024-10-23 18:49:40