Я использую библиотеку Curl для загрузки файла с сервера Apache.
Функция downloadFileWithCurlLibrary имеет два входа.
FileUrl - это файл, загружаемый с сервера форм.
Пример: www.abc.com/myfile.doc.
locFile - это каталог и имя файла, который вы хотите сохранить.
Пример: /home/projectx/myfile.doc.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <curl/curl.h>
int downloadFileWithCurlLibrary(char *FileUrl,char *locFile)
{
char dlFile[1024];
CURL *curl_handle;
FILE *pagefile;
memset(dlFile,0x0,sizeof(dlFile));
/* Create URL */
sprintf(dlFile,"%s",FileUrl);
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* Set timeout 3 min = 3*60 sec here. */
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 1800 );
/* set URL to get here */
if (curl_easy_setopt(curl_handle, CURLOPT_URL, dlFile) != CURLE_OK)
return -1;
/* Switch on full protocol/debug output while testing */
// if (curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L) != CURLE_OK)
// return -1;
/* disable progress meter, set to 0L to enable and disable debug output */
if (curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L) != CURLE_OK)
return -1;
/* send all data to this function */
if (curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data) != CURLE_OK)
return -1;
/* open the file */
pagefile = fopen(locFile, "wb");
if (pagefile)
{
/* write the page body to this file handle. CURLOPT_FILE is also known as
CURLOPT_WRITEDATA*/
if (curl_easy_setopt(curl_handle, CURLOPT_FILE, pagefile) != CURLE_OK)
return -1;
/* get it! */
if (curl_easy_perform(curl_handle) != 0)
return -1;
/* close the header file */
fclose(pagefile);
}
else
{
return -1;
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return 0;
}