2

I am trying to send a PDF file as a base64 string to my drive folder using google drive API v3, but the only thing I get as a result is a file which displays the base64 string.

My code :

const boundary = '-------314159265358979323846';
        const delimiter = "\r\n--" + boundary + "\r\n";
        const close_delim = "\r\n--" + boundary + "--";
        const metadata = {
            'name': 'myFile.pdf',
            'mimeType': 'application/pdf\r\n\r\n'
        };

        const multipartRequestBody = delimiter +
            'Content-Type: application/json\r\n\r\n' +
            JSON.stringify(metadata) +
            delimiter +
            'Content-Encoding: ' + 'base64\r\n' +
            'Content-Type: ' + 'application/pdf\r\n\r\n' +
            data +
            close_delim;

        const request = auth.request({
            url: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
            method: 'post',
            headers: {
                'Content-Type': `multipart/related; boundary=${boundary}`,
                'Content-Length': multipartRequestBody.length
            },
            data: multipartRequestBody
        });
        const response = await request;

I've searched everywhere, tried a lot of combinations but I am not able to make this work. Does anyone have an idea?

Devz
  • 563
  • 7
  • 23

1 Answers1

1

I have used following code succesfully to send pdfs and other types of files to the Google drive:

    var user = gapi.auth2.getAuthInstance().currentUser.get();
    var oauthToken = user.getAuthResponse(true).access_token;

    var boundary = 'foo_bar_baz';

    var data = '--' + boundary + '\n';
    data += 'content-type: application/json; charset=UTF-8' + '\n\n';
    data += JSON.stringify(metadata) + '\n';
    data += '--' + boundary + '\n';

    var dataURLparts = file.data.split(',', 2);
    var dataURLheaderParts = dataURLparts[0].split(':');
    var dataURLheaderPayloadParts = dataURLheaderParts[1].split(';');

    data += 'content-transfer-encoding: ' + dataURLheaderPayloadParts[1] + '\n';
    data += 'content-type: ' + dataURLheaderPayloadParts[0] + '\n\n';

    data += dataURLparts[1] + '\n';
    data += '--' + boundary + '--';

    console.log("uploading " + file.info.name);

    $.ajax({
        type: 'POST',
        url: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", "Bearer " + oauthToken);
        },
        contentType: 'multipart/related; boundary=' + boundary,
        data: data,
        processData: false
    }).done(function(response) {
        console.log(response);
    });

The file object in the example is from an HTML <input type="file"> element, so the content-type and the content-transfer-encoding can be get from the input element. There is other example where you can see what they look like for an image (base64 encoded): Send post request on Google API using javascript on browser. I am using jQuery to POST the data.

Orienteerix
  • 453
  • 1
  • 9
  • 16