You can use our HTTP and streams library to do this.
Please refer to the BingRequest sample, installed under %programfiles(x86)%\Microsoft Cpp REST SDK for VS 2012\SDK\samples\ (when you install the C++ REST SDK).
The steps would be:
- Open a file buffer in output mode (this will create a file on the disk)
- Send a GET request using the http_client, receive the http_response.
- Read the response body into the file buffer. This will download the file to the disk.
Here is the code snippet that will take care of this, I am using a local URL (http://localhost:60009/img.jpg
), replace this with the URL from which you want to download the file.
// Open a stream to the file to write the HTTP response body into.
auto fileBuffer = std::make_shared<streambuf<uint8_t>>();
return file_buffer<uint8_t>::open(L"img.jpg", std::ios::out ).then([=](streambuf<uint8_t> outFile) -> pplx::task<http_response>
*fileBuffer = outFile;
// Create an HTTP request.
// Write the response body into the file buffer.
.then([=](http_response response) -> pplx::task<size_t>
printf("Response status code %u returned.\n", response.status_code());
// Close the file buffer.
std::cout << datasize;
Let us know if this solves your issue.