Unicode C
Unicode C
Upload Binary File from Memory
See more Google Drive Examples
This example demonstrates how to upload a binary file from memory. It uploads the file to a subdirectory of our choosing.Chilkat Unicode C Downloads
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkCacheW.h>
#include <C_CkJsonArrayW.h>
#include <C_CkFileAccessW.h>
#include <C_CkByteData.h>
void ChilkatSample(void)
{
BOOL success;
HCkAuthGoogleW gAuth;
HCkRestW rest;
BOOL bAutoReconnect;
HCkJsonObjectW json;
HCkCacheW gdCache;
const wchar_t *folderId;
HCkJsonArrayW parents;
HCkFileAccessW fac;
HCkByteData jpgBytes;
const wchar_t *jsonResponse;
success = FALSE;
success = TRUE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// Google Drive scope.
gAuth = CkAuthGoogleW_Create();
CkAuthGoogleW_putAccessToken(gAuth,L"GOOGLE-DRIVE-ACCESS-TOKEN");
rest = CkRestW_Create();
// Connect using TLS.
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,bAutoReconnect);
// Provide the authentication credentials (i.e. the access token)
CkRestW_SetAuthGoogle(rest,gAuth);
// -------------------------------------------------------------------------
// A multipart upload to Google Drive needs a multipart/related Content-Type
CkRestW_AddHeader(rest,L"Content-Type",L"multipart/related");
// Specify each part of the request.
// The 1st part is JSON with information about the file.
CkRestW_putPartSelector(rest,L"1");
CkRestW_AddHeader(rest,L"Content-Type",L"application/json; charset=UTF-8");
// Construct the JSON that will contain the metadata about the file data to be uploaded...
json = CkJsonObjectW_Create();
CkJsonObjectW_AppendString(json,L"name",L"hedgehogs.jpg");
CkJsonObjectW_AppendString(json,L"description",L"A cute photo of two hedgehogs.");
CkJsonObjectW_AppendString(json,L"mimeType",L"image/jpeg");
// To place the file in a folder, we must add a parents[] array to the JSON
// and add the folder ID.
// In a previous example (see Build Local Metadata Cache
// we built a local cache to make it easy to lookup file IDs given a file path.
// Let's find the file ID for the folder "testFolder/abc/123"
gdCache = CkCacheW_Create();
CkCacheW_putLevel(gdCache,0);
CkCacheW_AddRoot(gdCache,L"C:/ckCache/googleDrive");
folderId = CkCacheW_fetchText(gdCache,L"testFolder/abc/123");
if (CkCacheW_getLastMethodSuccess(gdCache) != TRUE) {
wprintf(L"Filepath not found in cache.\n");
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkCacheW_Dispose(gdCache);
return;
}
parents = CkJsonArrayW_Create();
CkJsonObjectW_AppendArray2(json,L"parents",parents);
CkJsonArrayW_AddStringAt(parents,-1,folderId);
CkRestW_SetMultipartBodyString(rest,CkJsonObjectW_emit(json));
// The 2nd part is the file content, which will contain the binary image data.
CkRestW_putPartSelector(rest,L"2");
CkRestW_AddHeader(rest,L"Content-Type",L"image/jpeg");
fac = CkFileAccessW_Create();
jpgBytes = CkByteData_Create();
// Read a JPG file from the local filesystem.
success = CkFileAccessW_ReadEntireFile(fac,L"qa_data/jpg/hedgehogs.jpg",jpgBytes);
// Add the bytes to our upload
CkRestW_SetMultipartBodyBinary(rest,jpgBytes);
jsonResponse = CkRestW_fullRequestMultipart(rest,L"POST",L"/upload/drive/v3/files?uploadType=multipart");
if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkCacheW_Dispose(gdCache);
CkJsonArrayW_Dispose(parents);
CkFileAccessW_Dispose(fac);
CkByteData_Dispose(jpgBytes);
return;
}
// A successful response will have a status code equal to 200.
if (CkRestW_getResponseStatusCode(rest) != 200) {
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response JSON: %s\n",jsonResponse);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkCacheW_Dispose(gdCache);
CkJsonArrayW_Dispose(parents);
CkFileAccessW_Dispose(fac);
CkByteData_Dispose(jpgBytes);
return;
}
// Show the JSON response.
CkJsonObjectW_Load(json,jsonResponse);
// Show the full JSON response.
CkJsonObjectW_putEmitCompact(json,FALSE);
wprintf(L"%s\n",CkJsonObjectW_emit(json));
// A successful response looks like this:
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYoldmJ0Z3ZqT2x5MFk",
// "name": "hedgehogs.jpg",
// "mimeType": "image/jpeg"
// }
// Get the fileId:
wprintf(L"fileId: %s\n",CkJsonObjectW_stringOf(json,L"id"));
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkCacheW_Dispose(gdCache);
CkJsonArrayW_Dispose(parents);
CkFileAccessW_Dispose(fac);
CkByteData_Dispose(jpgBytes);
}