Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

308
Visualizações
google script create file from external blob

I'm getting a PDF file from an external web resource using ajax. I want to store this PDF in the google drive using google script API from within google docs.

Sample of that ajax call:

 $.ajax({
      url: "<fancyurl>",
      contentType: 'application/octet-stream',
      responsetype: 'blob',
      type: 'GET',
      success: function(response) {

 // or converted response.. etc.. 

google.script.run.withSuccessHandler(yay).withUserObject(this).createFile(response);

});

The response from the webresource is an octet-stream:

enter image description here

I've tried to import the original response, the response converted to blob, a uint8 blob and the base64string. All end up in errors or corrupt files.

   var blob = Utilities.newBlob(data, 'application/pdf' ,'asdf.pdf');
   // blob.setName('asdf.pdf');
   // blob.setContentTypeFromExtension();
   DriveApp.createFile(blob);

Where data is the response or converted response.

Does anybody know how to solve this or what google script expects as a valid input?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In my experience, when the binary data is retrieved using ajax, the binary data is converted to the text data. By this, I'm worried that responsetype of blob and arraybuffer might not be able to be used. I thought that this might be the reason of your issue. So, in this answer, I would like to propose using "XMLHttpRequest" instead of "ajax".

The modified script is as follows.

Modified script:

From:

 $.ajax({
      url: "<fancyurl>",
      contentType: 'application/octet-stream',
      responsetype: 'blob',
      type: 'GET',
      success: function(response) {

 // or converted response.. etc.. 

google.script.run.withSuccessHandler(yay).withUserObject(this).createFile(response);

});

To:

const xhr = new XMLHttpRequest();
xhr.open('GET', "<fancyurl>", true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
  google.script.run.withSuccessHandler(yay).withUserObject(this).createFile([...new Int8Array(this.response)]);
};
xhr.send();

In this modification, the binary data is converted to the array buffer and converted it to int8 array. By this, this data can be used as the byte array with Google Apps Script.

In this case, although I cannot see your whole script of Google Apps Script, you can use your Google Apps Script as follows.

function createFile(data) {
  var blob = Utilities.newBlob(data, 'application/pdf', 'asdf.pdf');
  DriveApp.createFile(blob);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda