I'm receiving a base64encoded image in a Google Sheets script. I can successfully generate an image by logging
e.postData.contents
then using https://www.base64decode.org/ to decode and generate a .jpg file. But when I try to do the decoding inside the google script and write a jpg file to drive, it is corrupted. It looks similar, but there are some replacement characters inserted. Here's my code:
function doPost(e) {
var decoded = Utilities.base64Decode(e.postData.contents, Utilities.Charset.UTF_8);
var blob = Utilities.newBlob(decoded);
DriveApp.createFile('img_'+date+'.jpg', blob.getDataAsString('Windows-1252'), MimeType.JPEG);
}
I also tried blob.getAs('image/jpeg')
, but that just returns a 4-byte file containing the text Blob
.
Any ideas? I've tried a few different charsets, and Windows-1252
seems to give the closest results.
How about this sample script? When you use this, please define date
. In my test, also I could confirm that the base64 encoded jpeg file could be decoded at https://www.base64decode.org/
.
And when you updated doPost()
, please be careful as follows. Please redeploy your script to Web Apps as a new version. If this is not carried out, script of the deployed Web Apps is not updated.
function doPost(e) {
var decoded = Utilities.base64Decode(e.parameters.contents, Utilities.Charset.UTF_8);
var blob = Utilities.newBlob(decoded, "image/jpeg", 'img_'+date+'.jpg'); // Please define date.
DriveApp.createFile(blob);
}
If you want to try your doPost()
using curl, you can use following command. When you use this, please input filename which is base64 encoded file and Web Apps URL.
curl -L -F "contents=`cat ### Filename ###`" "https://script.google.com/macros/s/#####/exec"
If I misunderstand your question, I'm sorry.