下载文件扩展 - 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 | int64_t | OLAPlug对象的指针 |
| url | string | 完整URL |
| save_path | string | 本地保存路径 |
| callback | DownloadCallback | 下载进度回调函数(详见下方回调函数说明) |
| user_data | int64_t | 传给callback的用户数据 |
回调函数类型定义
void DownloadCallback(int64_t current, int64_t total, int64_t speed, int64_t user_data);
回调函数说明:在文件下载过程中,系统会定期调用此类型的回调函数以报告下载进度。
回调参数说明:
| 参数名 | 类型 | 说明 |
|---|---|---|
| current | int64_t | 已下载字节数 |
| total | int64_t | 总字节数(0 表示未知,通常是服务器未提供Content-Length) |
| speed | int64_t | 当前下载速度(字节/秒) |
| user_data | int64_t | 由调用方传入的用户数据,可用于传递上下文信息(如窗口句柄、对象指针等) |
| max_retries | int32_t | 断线后最大重试次数 |
| connect_timeout_sec | int32_t | 连接超时秒数,0使用默认值 |
| read_timeout_sec | int32_t | 读取超时秒数,0使用默认值 |
示例
// 下载进度回调
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 if (result == -5) {
printf("下载超时\n");
} else if (result == -4) {
printf("连接失败\n");
} else {
printf("下载失败,错误码:%d\n", result);
}
返回值
| 返回值类型 | 说明 |
|---|---|
| int32_t | 错误码,0表示成功,负数表示失败(参见错误码表) |
注意事项
- 支持断点续传和自动重试
- 重试次数不包括首次尝试
- 超时设置为0时使用默认值(通常为30秒)
- 适合下载大文件或网络不稳定的场景
