Upload ArrayBuffer content to Google Drive using Google Drive API v3 and Javascript

I am trying to create a file in Google Drive with the contents of an ArrayBuffer. I am using Javascript and version 3 on the aPI disk.

I managed to create a filled text file using this sample code , but I cannot find any example of how to fill a file using binary data from ArrayBuffer. Is this possible, or do I need to encode my binary data as a string before I upload it to Google Drive?

The Google documentation for v3 is really missing: - (

Any help is appreciated.

+4
source share
1 answer

, ArrayBuffer UTF-8. :

// file.data is an ArrayBuffer
var b64str = btoa(String.fromCharCode.apply(null, new Uint8Array(file.data)));

Content-Transfer-Encoding api, :

// 'data' is our base64'd image that came from ArrayBuffer
var requestBody =
    '--' + boundary_string + '\n' +
    "Content-Type: application/json; charset=UTF-8\n\n" +
    JSON.stringify(metadata) + '\n' +
    '--' + boundary_string + '\n' +
    "Content-Type: " + mime + "\n" +
    "Content-Transfer-Encoding: base64" +
    data + '\n' +
    '--' + boundary_string + '--';
0

Source: https://habr.com/ru/post/1659820/


All Articles