I want to save the base64 image in Google Apps Script.

Asked 2 years ago, Updated 2 years ago, 82 views

I would like to decode the image encoded in base64 in gas and save it in blob.

However, the seventh line of Utilities.base64Decode(data); displays the error Failed to decode the string.

I tried other base64s, but the results didn't change.

The base64 in the code below is Wikipedia-Data URI scheme to
Others were encoded with a service such as Base64 encoder.

If you have any solutions or alternatives, please take care of them.

function writeFile(){  
  vardrive=DriveApp.getFolderById('<<folder id>>');
  varfileName='sample.png';
  var contentType='image/png';
  vardata='data:image/png;base64,iVBORw0KGgoAAAAANSUhEugAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA  

  vardecoded=Utilities.base64Decode(data);

  varblob=Utilities.newBlob(decoded, contentType, fileName)

  drive.createFile(blob);
}

javascript google-apps-script base64

2022-09-30 19:44

1 Answers

The base64 encoded string substituting the variable data is invalid.

I think it would be OK if the first string excluding "data:image/png;base64," was used as the base64 Decode argument.


2022-09-30 19:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.