利用curl下载文件(进度条显示)

上传人:油条 文档编号:20442399 上传时间:2017-11-22 格式:DOC 页数:6 大小:176KB
返回 下载 相关 举报
利用curl下载文件(进度条显示)_第1页
第1页 / 共6页
利用curl下载文件(进度条显示)_第2页
第2页 / 共6页
利用curl下载文件(进度条显示)_第3页
第3页 / 共6页
利用curl下载文件(进度条显示)_第4页
第4页 / 共6页
利用curl下载文件(进度条显示)_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《利用curl下载文件(进度条显示)》由会员分享,可在线阅读,更多相关《利用curl下载文件(进度条显示)(6页珍藏版)》请在金锄头文库上搜索。

1、在项目中需要用到程序更新的功能,同事介绍说是 curl 中的开发库很不错,于是下载这个包测试了一下,确实不错。准备正式用到项目中,以下一个例子用于从互联网上抓取一个文件下载到本地,并加上进度条显示,做得挺简陋,不过功能差不多就这样了。程序运行预览.首先需要加入多线程的机制,因为程序一边在下载文件,一边在显示进度条,单线程的方式肯定不行,所以我用到了 wxTimer 来实现,在 downloadMain.h 中定义了一个 wxTimer,并做了事件申明.DECLARE_EVENT_TABLE() /* * Name: downloadMain.h * Purpose: Defines Appli

2、cation Frame * Author: () * Created: 2008-11-14 * Copyright: () * License: */ #ifndef DOWNLOADMAIN_H #define DOWNLOADMAIN_H #include downloadApp.h #include #include GUIDialog.h class downloadDialog: public GUIDialog public: downloadDialog(wxDialog *dlg); downloadDialog(); void .Timer(wxTimerEvent& e

3、vent); private: virtual void .Close(wxCloseEvent& event); virtual void .Quit(wxCommandEvent& event); virtual void .About(wxCommandEvent& event); void downloadfile(); wxTimer* m_timerdown; DECLARE_EVENT_TABLE() ; #endif / DOWNLOADMAIN_H 下面是主程序的代码./* * Name: downloadMain.cpp * Purpose: Code for Applic

4、ation Frame * Author: (alan) * Created: 2008-11-14 * License: */ #ifdef WX_PRECOMP #include wx_pch.h #endif #ifdef _BORLANDC_ #pragma hdrstop #endif /_BORLANDC_ #include downloadMain.h #include #include #include #include update.h #include #include #define TIMER_ID 22222 /事件监听声明 BEGIN_EVENT_TABLE(dow

5、nloadDialog, GUIDialog) EVT_TIMER(TIMER_ID, downloadDialog:OnTimer) END_EVENT_TABLE() enum wxbuildinfoformat short_f, long_f ; wxString wxbuildinfo(wxbuildinfoformat format) wxString wxbuild(wxVERSION_STRING); if (format = long_f ) #if defined(_WXMSW_) wxbuild Start(100); downloadDialog:downloadDial

6、og() /定时器操作 void downloadDialog:OnTimer(wxTimerEvent &event) downloadfile(); /文件写入流 int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) struct FtpFile *out=(struct FtpFile *)stream; if (out & !out-stream) out-stream=fopen(out-filename, wb); if (!out-stream) return -1; return fwrite(

7、buffer, size, nmemb, out-stream); /进度条显示函数 int wxcurldav_dl_progress_func(void* ptr, double rDlTotal, double rDlNow, double rUlTotal, double rUlNow) wxGauge* pGauge = (wxGauge*) ptr; if(pGauge) /设置进度条的值 pGauge-SetValue(100.0 * (rDlNow/rDlTotal); return 0; /下载文件函数 void downloadDialog:downloadfile() /

8、创建 curl 对象 CURL *curl; CURLcode res; m_staticText2-SetLabel(wxT(请耐心等待程序下载更新包.); struct FtpFile ftpfile= /定义下载到本地的文件位置和路径 tmp.exe,NULL ; curl_global_init(CURL_GLOBAL_DEFAULT); /curl 初始化 curl = curl_easy_init(); /curl 对象存在的情况下执行操作 if (curl) /设置远端地址 curl_easy_setopt(curl, CURLOPT_URL,http:/dl_ /执行写入文件流

9、操作 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); /curl 的进度条声明 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); /回调进度条函数 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, wxcurldav_dl_progress_func); /设置进度条名称 curl_easy_setopt(curl, CUR

10、LOPT_PROGRESSDATA, m_gauge1); /进度条 m_gauge1-SetValue(0); /写入文件 res = curl_easy_perform(curl); m_gauge1-SetValue(100); /释放 curl 对象 curl_easy_cleanup(curl); if (CURLE_OK != res) ; if (ftpfile.stream) /关闭文件流 fclose(ftpfile.stream); /释放全局 curl 对象 curl_global_cleanup(); /这一步很重要,停止定时器,不然程序会无休止的运行下去 m_timerdown-Stop(); /执行刚下载完毕的程序,进行程序更新 int pid = :wxExecute(_T(tmp.exe); wxMessageBox(wxT(下载完毕,程序开始执行更新操作.); void downloadDialog:OnClose(wxCloseEvent &event) Destroy(); void downloadDialog:OnQuit(wxCommandEvent &event) Destroy(); void downloadDialog:OnAbout(wxCommandEvent &event)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号