Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkHttp.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkBinData bd;
    int statusCode;
    const char *base64_image_data;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    http = CkHttp_Create();
    CkHttp_putKeepResponseBody(http,TRUE);

    bd = CkBinData_Create();

    success = CkHttp_DownloadBd(http,"https://www.chilkatsoft.com/images/starfish.jpg",bd);
    statusCode = CkHttp_getLastStatus(http);
    if (success == FALSE) {
        if (statusCode == 0) {
            //  Unable to either send the request or get the response.
            printf("%s\n",CkHttp_lastErrorText(http));
        }
        else {
            //  We got a response, but the status code was not in the 200s
            printf("Response status code: %d\n",statusCode);
            //  Examine the response body.
            printf("Response body:\n");
            printf("%s\n",CkHttp_lastResponseBody(http));
        }

        printf("Download failed.\n");

    }
    else {
        printf("Download success, response status = %d\n",statusCode);

        base64_image_data = CkBinData_getEncoded(bd,"base64");
        printf("image data in base64 format:\n");
        printf("%s\n",base64_image_data);
    }



    CkHttp_Dispose(http);
    CkBinData_Dispose(bd);

    }