Reputation: 1833
I develop C++ application. It downloads files. Typical url looks like https://mysite.com/download.php?fileid=xxx So the first problem - I need to get real filename. I do it like:
curl_easy_setopt(pCurl, CURLOPT_HEADERFUNCTION, readHeader);
And a callback
size_t NetworkHelper::readHeader(void *ptr, size_t size, size_t nmemb, void *data)
{
std::string header;
header.assign((char*)ptr, nmemb);
size_t delimPos = header.find_first_of(':');
std::string headerName;
std::string headerContent;
if (delimPos != std::string::npos)
{
headerName = header.substr(0, delimPos);
headerContent = header.substr(delimPos + 1, header.length());
m_headers[headerName] = headerContent;
}
return size*nmemb;
}
Here I insert headers and its values into map this callback is invoked many times for each http header. The header which I really need is "Location: url/realfilename.yyy".
And writing callback:
size_t NetworkHelper::writeFileCallback(void* ptr, size_t size, size_t nmemb, void* data)
{
size_t count;
Fetchdata* fetchdata;
fetchdata = (Fetchdata*)data;
std::string fileName = NetworkHelper::m_headers["Location"].c_str();
fileName = "C:/" + fileName.substr(fileName.find_last_of('/') + 1, fileName.length());
fetchdata->stream = fopen( fileName.c_str(),"w+b");
if (fetchdata->stream!=NULL)
{
count = fwrite(ptr, size, nmemb, fetchdata->stream);
if (count > 0)
{
fetchdata->size += (count * size);
}
}
return nmemb*size;
}
And here fopen fails (returns NULL). Is it wrong how I process files and how to do it better way? Thanks.
Upvotes: 1
Views: 455
Reputation: 11
At first check what fileName actually contains. Use error() or GetLastError on windows to check what's the problem. I may be wrong, but according to your code fileName actually contains something like this: C:/http://somesimet.somedomain/file.extension upd: sorry, was really wrong about file name
Upvotes: 1