ProgressBar bar;
public:
int download(const char* url, const char* filename, int enable_progress);
int download_progress(void *p, double dl, double dlnow, double ul, double ulnow);
int myClass::download_progress(void *cli, double dl, double dlnow, double ul, double ulnow)
double p = (dl / dlnow) * 100;
myClass *mp = (myClass *)cli;
mp->percent = (int)dl;
mp->bar.setVal(p)
return 0;
int myClass::download(const char* url, const char* filename)
int res = 0;
CURL* handle = curl_easy_init();
if (handle == NULL) {
return -1;
FILE* f = fopen(filename, "wb");
if (!f) {
return -1;
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, f);
curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, (curl_progress_callback)&myClass::download_progress);
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(handle, CURLOPT_USERAGENT, "InetURL/1.0");
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_perform(handle);
curl_easy_cleanup(handle);
off_t sz = ftello(f);
fclose(f);
if ((sz == 0) || (sz == (off_t)-1)) {
res = -1;
remove(filename);
return res;
Thanks
2
个回答
0
人赞同
首先你需要做的是
&myClass::download_progress
变成一个静态函数,并简单地调用它,如。
&download_progress
0
人赞同
你没有发送要调用的userdata。你的下载方法应该是这样的。
curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, (curl_progress_callback)&myClass::download_progress);
curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, this);
另外,你的download_progress应该被标记为静态。
class myClass{
int percent;
ProgressBar bar;
public: