0

I'm passing data to the google application wich published on the web using Post method. Here is the code of the sever side:

function doPost(e) {
  var params, result, responce;
  if(typeof e !== 'undefined')
    params = (e.parameters);
    if (params['type'] == 'get') {
     //do the get;
    }
    else {
    try {
      result = reformat_src(e.parameters);
      responce = process_result(result);
    }
    catch(e) {
      return ContentService.createTextOutput(JSON.stringify(e));
    }
    }
    return ContentService.createTextOutput(JSON.stringify(responce));

}

function reformat_src(request) {
  var prop, obj;
  obj = {};
  for (prop in request) {
    obj[prop] = request[prop][0];
  }
  return obj;
}

In process_result function I'm trying to create an image file on google drive

function process_result(result) {
  var folder;
  folder = DriveApp.getRootFolder();
  folder.createFile('test', result['blob'], MimeType.PNG);
}

I know that result['blob'] contains the png file that I've got with

var reader = new FileReader();
reader.readAsBinaryString(f);

But I failed to create a file. Does anybody know how to create an image file having data from FileReader? I can pass to GAS any of type:
readAsBinaryString
readAsText
readAsDataURL
readAsArrayBuffer Thank you in advance!

Karen Fisher
  • 747
  • 1
  • 8
  • 25

1 Answers1

0

As usual I found answer by myself. I need to use readAsDataURL and this function:

function uploadArquivoParaDrive(base64Data, nomeArq, idPasta) {
  try{
    var splitBase = base64Data.split(','),
        type = splitBase[0].split(';')[0].replace('data:','');

    var byteCharacters = Utilities.base64Decode(splitBase[1]);
    var ss = Utilities.newBlob(byteCharacters, type);
    ss.setName(nomeArq);

    var file = DriveApp.getFolderById(idPasta).createFile(ss);

    return file.getName();
  }catch(e){
    return 'Erro: ' + e.toString();
  }
}

found here

Community
  • 1
  • 1
Karen Fisher
  • 747
  • 1
  • 8
  • 25