下载文件扩展 - HttpDownloadFileEx
函数简介
下载文件,支持断点续传、进度回调、重试和超时设置。
接口名称
HttpDownloadFileEx
DLL调用
int32_t HttpDownloadFileEx(int64_t instance, const char* url, const char* save_path, DownloadCallback callback, int64_t user_data, int32_t max_retries, int32_t connect_timeout_sec, int32_t read_timeout_sec);
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| url | 字符串 | 完整URL |
| save_path | 字符串 | 本地保存路径 |
| callback | DownloadCallback | 下载进度回调函数 |
| user_data | 长整数型 | 传给callback的用户数据 |
| max_retries | 整数型 | 断线后最大重试次数 |
| connect_timeout_sec | 整数型 | 连接超时秒数,0使用默认值 |
| read_timeout_sec | 整数型 | 读取超时秒数,0使用默认值 |
回调函数类型定义
void DownloadCallback(int64_t current, int64_t total, int64_t speed, int64_t user_data);
| 参数名 | 类型 | 说明 |
|---|---|---|
| current | 长整数型 | 已下载字节数 |
| total | 长整数型 | 总字节数(0表示未知) |
| speed | 长整数型 | 当前下载速度(字节/秒) |
| user_data | 长整数型 | 由调用方传入的用户数据 |
示例
// 下载进度回调
void OnDownloadProgress(int64_t current, int64_t total, int64_t speed, int64_t user_data) {
if (total > 0) {
printf("进度:%.2f%%\n", (double)current / total * 100.0);
}
}
// 下载文件,最多重试3次,连接超时30秒,读取超时60秒
int32_t result = HttpDownloadFileEx(instance,
"https://example.com/large-file.zip",
"D:/downloads/large-file.zip",
OnDownloadProgress,
0,
3, // 最多重试3次
30, // 连接超时30秒
60); // 读取超时60秒
if (result == 0) {
printf("下载成功\n");
} else {
printf("下载失败,错误码:%d\n", result);
}
返回值
整数型,错误码,0表示成功,负数表示失败(错误码与 HttpDownloadFile 一致)。
注意事项
- 支持断点续传和自动重试
- 重试次数不包括首次尝试
- 超时设置为0时使用默认值(通常为30秒)
- 适合下载大文件或网络不稳定的场景
