Ich benutze Curl-Bibliothek, um einen einfachen C-Code mit MSVC zu erstellen, um eine Datei von einer URL herunterzuladen. Das Problem ist, wenn die Verbindung in der Mitte des Downloads bricht mein Code wird einfrieren und die unvollendete Datei wurde nicht aus dem Verzeichnis entfernt. Was ich will ist, wenn der Download fehlgeschlagen ist, muss das Programm die Verbindung erneut versuchen oder entfernen Sie die unfertige Datei und versuchen Sie es erneut. Ich bevorzuge lieber C-Bibliotheken als C++ - Bibliotheken. Hier ist der Code ich verwende:unfertigen Download mit Curl in C
//lib for curl
#include <curl/curl.h>
#define CURL_STATICLIB
bool downloader3(string url, string file_path) {
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if (curl) {
fp = fopen(file_path.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
//always cleanup
curl_easy_cleanup(curl);
fclose(fp);
double val;
res = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &val);
if ((CURLE_OK == res) && (val>0))
printf("Average download speed: %0.3f kbyte/sec.\n", val/1024);
if ((res == CURLE_OK)) {
printf("Download Successful!\r\n");
return true;
}
else {
printf("Downlaod Failed!\r\n");
remove(file_path.c_str()); //remove the temp file
return false;
}
}
}
EDIT --- Dank Ring Ø Antwort. Ich habe den Code geändert, aber ich suche nach einer Fortsetzungsfunktion, die den Download unvollständiger Dateien fortsetzen kann.
bool downloader3(string url, string file_path) {
CURL *curl;
FILE *fp = NULL;
CURLcode res;
int status;
int maxtries = 3;
do {
printf("Doing try # %d\r\n", maxtries);
curl = curl_easy_init();
if (curl) {
fp = fopen(file_path.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); // 30 seconds
res = curl_easy_perform(curl);
//always cleanup
curl_easy_cleanup(curl);
fclose(fp);
if ((res == CURLE_OK)) {
printf("Download Successful!\r\n");
break;
//return true;
}
}
} while (--maxtries);
if (maxtries) { // was OK
//curl_easy_cleanup(curl); // clean curl/delete file?
//fclose(fp);
return true;
}
else {
printf("Download Failed!\r\n");
printf("file path is: %s", file_path.c_str());
Sleep(5000);
status = remove(file_path.c_str()); //remove the unfinished file
if (status == 0)
printf("%s file deleted successfully.\n", file_path);
else
{
printf("Unable to delete the file\n");
}
return false;
}
}
Danke, aber ich weiß nicht, die genaue Zeit den Download, zum Beispiel zu beenden, wenn der Benutzer eine Datei mit 10KB/s herunterlädt es würde länger dauern. –
Bitte lesen Sie die bearbeitete Antwort. –
Ich habe Ihren Code versucht, aber die unfertige Datei ist immer noch da. –