Unicode C
Unicode C
Download Image (JPG, GIF, etc.) to Base64
See more HTTP Examples
Demonstrates how to download an image, or any type of file, to get the data in base64 encoding format.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkBinDataW bd;
int statusCode;
const wchar_t *base64_image_data;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
CkHttpW_putKeepResponseBody(http,TRUE);
bd = CkBinDataW_Create();
success = CkHttpW_DownloadBd(http,L"https://www.chilkatsoft.com/images/starfish.jpg",bd);
statusCode = CkHttpW_getLastStatus(http);
if (success == FALSE) {
if (statusCode == 0) {
// Unable to either send the request or get the response.
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
}
else {
// We got a response, but the status code was not in the 200s
wprintf(L"Response status code: %d\n",statusCode);
// Examine the response body.
wprintf(L"Response body:\n");
wprintf(L"%s\n",CkHttpW_lastResponseBody(http));
}
wprintf(L"Download failed.\n");
}
else {
wprintf(L"Download success, response status = %d\n",statusCode);
base64_image_data = CkBinDataW_getEncoded(bd,L"base64");
wprintf(L"image data in base64 format:\n");
wprintf(L"%s\n",base64_image_data);
}
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bd);
}