Elangamani
Elangamani

Reputation: 33

Convert Drive image to base 64 encoding for upload using google app script

Get file from drive and convert it to base64 format using google app script. I have used getFileById method from DriveApp. I got file size, file name and mime type. but am not able to get the image content to encode to base 64.

fileId = '1kLLkY_0TYs01o32q26uPQ_ndzaOQRmhi';
var file = DriveApp.getFileById(fileId);
Logger.log(file);
var docName = file.getName();
var docSize = file.getSize();
var docType = file.getMimeType();

var docImg = Utilities.base64Encode(file.getBlob());
Logger.log(docImg);


fileId = '1kLLkY_0TYs01o32q26uPQ_ndzaOQRmhi';
var file = DriveApp.getFileById(fileId);
Logger.log(file);
var docName = file.getName();
var docSize = file.getSize();
var docType = file.getMimeType();

var docImg = Utilities.base64Encode(file.getBlob());
Logger.log(docImg);

I expected to get exact image to convert base 64 format, then only i can upload to my server.

Upvotes: 1

Views: 2407

Answers (1)

ziganotschka
ziganotschka

Reputation: 26796

Google Apps Script reference specifies that

the method Utilities.base64Encode() generates a base-64 encoded string from a byte array.

Thus, you need to get your blob as bytes first, before encoding it:

var docImg = Utilities.base64Encode(file.getBlob().getBytes());

Upvotes: 2

Related Questions