下载文件,支持断点续传与进度回调。
int32_t HttpDownloadFile(int64_t instance, const char* url, const char* save_path, DownloadCallback callback, int64_t user_data)
| 参数名 | 类型 | 说明 |
|---|
| instance | int64_t | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成 |
| 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 | 由调用方传入的用户数据,可用于传递上下文信息(如窗口句柄、对象指针等) |
| 错误码 | 说明 |
|---|
| 0 | 下载成功 |
| -1 | 无效的URL |
| -2 | 无法创建文件 |
| -3 | 无法写入文件 |
| -4 | 连接失败 |
| -5 | 超时 |
| -6 | DNS解析失败 |
| -7 | SSL/TLS错误 |
| -404 | 404文件不存在 |
| -403 | 403禁止访问 |
| -500 | 500服务器错误 |
| -502 | 502网关错误 |
| -503 | 503服务不可用 |
| -999 | 未知错误 |
void OnDownloadProgress(int64_t current, int64_t total, int64_t speed, int64_t user_data) {
if (total > 0) {
double progress = (double)current / total * 100.0;
printf("下载进度:%.2f%% (%.2f MB / %.2f MB) 速度:%.2f KB/s\n",
progress,
current / 1024.0 / 1024.0,
total / 1024.0 / 1024.0,
speed / 1024.0);
} else {
printf("已下载:%.2f MB 速度:%.2f KB/s\n",
current / 1024.0 / 1024.0,
speed / 1024.0);
}
}
int32_t result = HttpDownloadFile(instance,
"https://example.com/file.zip",
"D:/downloads/file.zip",
OnDownloadProgress,
0);
if (result == 0) {
printf("下载成功\n");
} else {
printf("下载失败,错误码:%d\n", result);
}
| 返回值类型 | 说明 |
|---|
| int32_t | 错误码,0表示成功,负数表示失败(参见错误码表) |
- 支持断点续传,如果文件已部分下载,会自动从断点处继续
- 回调函数在下载过程中会被多次调用
- 如果不需要进度回调,可以传NULL
- user_data可以传递任意用户数据,如窗口句柄、对象指针等
- 支持HTTP和HTTPS协议