libcurl上传、下载获取进度

获取上传、下载的进度,可以有多种方法。有的方法简单,有的方法就很复杂。比如这两天应产品要求,在原有的上传、下载的基础上,增加进度的获取。我就是先顺着原来的思路,去用了复杂的办法。这个方法,大致的思路就是,在读或者写的回调函数中,去计算进度。以下载为例吧,首先获取服务器中文件的大小,然后设计一个结构体,结构体中有参数,totalSize(完整文件的字节数),transferSize(已传输数据的字节数),percent(传输的百分比)。后者在读回调函数中,自己去累加。如下:

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)

{

curl_off_t nread;

curlFile* callbackPara = (curlFile*)stream;

size_t retcode = fread(ptr, size, nmemb, callbackPara->stream);

nread = (curl_off_t)retcode;//本次实际传输的字节数

callbackPara->transferSize = callbackPara->transferSize + retcode;//对传输的字节进行累计

if(callbackPara->percent)

{

//计算百分比

*(callbackPara->percent) = callbackPara->transferSize*100.0/callbackPara->totalSize;

}

return retcode;

}

实际用起来,这个方法就很二。比如如果通过ftp去下载,还得先写一个函数,去获取目标文件的大小。不过这个也实现了,真正把我给卡住的是,通过http post方法去上传文件。

照着curl的demo,常规操作,如下:

curl_easy_setopt(curl, CURLOPT_URL, url);

curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);//回调函数

curl_easy_setopt(curl, CURLOPT_READDATA, &callBackPara);//回调的参数

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");

curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,(curl_off_t)fsize);

curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false

除此之外,还设置了CURLOPT_POSTFIELDS。

然后测试下来,就发现上传失败了。但是回调函数是被调用了,而且显示数据也被读取了。但是传输就是失败。

如果不设置回调函数,文件就能上传成功。最后,在curl的文档中翻到了下面这一段:

Optionally, you can provide data to POST using the CURLOPT_READFUNCTION and CURLOPT_READDATA options but then you must make sure to not set CURLOPT_POSTFIELDS to anything but NULL. When providing data with a callback, you must transmit it using chunked transfer-encoding or you must set the size of the data with the CURLOPT_POSTFIELDSIZE or CURLOPT_POSTFIELDSIZE_LARGE options. To enable chunked encoding, you simply pass in the appropriate Transfer-Encoding header, see the post-callback.c example.

所以,在我的代码中,因为CURLOPT_POSTFIELDS不为空,所以就出现了异样。在这种场景下,花了几个小时,想办法通过调整参数,来实现目的,最后都没有实现。在这种场景下,复杂的方法没有奏效。

复杂的方法没有奏效后,改为使用简单的办法。其实简单的办法,还是在curl文档中翻到的,还是偶然。简单来说,就是下面这段:

curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);

curl_easy_setopt(curl, CURLOPT_XFERINFODATA, percent);

curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);

关键是,这种方法适用于所有场景,只是上传或者下载的时候,选对参数就可以。而且,使用这种方法,比如去做ftp下载,不需要提前获取文件的大小。

所以,磨刀不误砍柴工啊。对curl这个库,要加深理解,用的时候方能事半功倍。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用libcurl库来实现下载进度的跟踪。下面是一个使用libcurl下载文件并显示进度的示例代码: ```c #include <stdio.h> #include <curl/curl.h> // 回调函数,用于跟踪下载进度 int progress_callback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { // 计算下载进度百分比 double progress = dlnow / (double)dltotal * 100.0; printf("下载进度: %.2f%%\r", progress); fflush(stdout); // 刷新输出缓冲区 return 0; } int main() { CURL* curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { // 设置下载地址 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/file_to_download"); // 设置进度回调函数 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); res = curl_easy_perform(curl); if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } ``` 在上面的示例中,我们首先定义了一个名为`progress_callback`的回调函数,该函数在每个下载进度数据更新时被调用。然后,在主函数中,我们使用`curl_easy_setopt`函数设置了进度回调函数,并通过`curl_easy_perform`函数开始下载。 当你运行上述代码时,它将显示下载进度,类似于以下输出: ``` 下载进度: 50.00% ``` 你可以根据需要自定义进度显示的格式和其他操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值