Help

Re: How do run scripting app whenever a new record is created?

Solved
Jump to Solution
1554 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Rohith_S
6 - Interface Innovator
6 - Interface Innovator

Hello,

So, I need to convert attachment file URL to base64 string, as btoa() function does not work in automation script, I am running a scripting app that converts URL to Base64 string and the output of the scripting app will be entered into a new column I created as “base64 string” and using this value in the automation script.

My question is how do I run scripting app every time a record is created?

Or even better is there any way to convert attachment URL to base64 in the automation script?

1 Solution

Accepted Solutions

If you have an image in base64 format and want it to be an attachment in Airtable, you can use an conversion service (like CloudConvert) to convert the raw text into an image file, and then upload the image file into an attachment field.

But it sounds like this is not what the OP wants to do.

It sounds like the OP’s customer will be uploading photos to an attachment field. Scripting app always needs to be run manually, so this won’t work unless you are okay with having a big delay until someone can push that button. Also, the base64 version of an image is likely too large to fit in a long text field. You would have to generate a text file with the base64 info at a public url, and then move that file to a different attachment field. That’s too complicated.

Interestingly enough, the code int the Web Dev link that Bill shared actually works in Scripting app, although the editor complains that it doesn’t know what a FileReader is. On the other hand, the exact same code doesn’t work in an automaton script. You can create the FileReader, but FileReader.readAsDataUrl is not supported.

This sounds like the best option.

See Solution in Thread

10 Replies 10

@Rohith_S, if you really mean Base24, hats off - you are a very enlightened developer. :winking_face:

I have not tried this in a script automation, but I give it at least a 51% probability it will work. Simple include this code in your automation script and call it.

@Bill.French Oh no, sorry. My Bad.
base64 not base24, basically I want to convert attachment URL to base64 string in automation script, is there any way to do that because btoa() isn’t working in automation script
I have edited the question.

Still no. You need to encode in native JavaScript to do this or adopt another encoding strategy.

The answer is still the same - you either need to use native javascript to create encoding or you need to use a different encoding method.

@Bill.French , Yes, I have used the code that you suggested in other answer, but now it is converting string to base64, the problem is I want to convert the contents of the URL(Image) to base64, I want the Image uploaded in the attachment column in base64 not the URL, the content in the URL i.e, Image.
So the conversion I am looking for is URL to base64, not string to base64.
To use JavaScript, many functions are not supported in Aitable Scripting like FileReader() or creating canvas element, I am having a hard time doing this.

Thanks for sharing the additional requirements. Earlier, you said:

I want to convert attachment URL to base64 string in automation script

This is possible; you simply need to build a base64 encoding function of your own that mimicks atob().

I want the Image uploaded in the attachment column in base64 not the URL

In contrast, this is not possible. The advent of these new deeper insights changes all my answers to simply “no”. :winking_face: Airtable (to the best of my knowledge) doesn’t provide any methods to inject images into attachment fields as base64 files or as any other format for that matter.

@kuovonne might have some deeper knowledge on this topic but I’m pretty sure, however elegant your approach may be, this is not possible.

@Bill.French , Sorry, I think I might have not explained it properly, Let me Explain the whole flow.

So the customer uploads their photo in the attachment column in Airtable and I have to use the photo and send a POST request to my website where I send Photo as Payload from airtable, but my website accepts only base64 as input for image, so I have to send image in base64 format for my website from airtable.
In Airtable when a photo is attached in attachment column, it creates a separate Airtable URL for the photo, this URL when used will display the photo in browser.
My question is, Is there any possible way to convert the photo uploaded in Airtable to base64 using the airtable URL of the photo uploaded and use that to send POST request.

Thanks! It’s a lot better when you share all the requirements. :winking_face:

First you must fetch the binary value of the image from Airtable’s URL location. Then you must convert that image to a base64 string. Since you cannot use btoa() in the script automation, you must write a custom base64 function which is a lot of work and complex. Then you must post the payload containing the base64 string.

One alternative is to use a script block to always keep a base64 version of the image in a text field. This has some issues if the image is large. Another alternative is to create a web service using something like AutoCode that converts images into base64 strings that you can call in the website upload process. Another approach is to simply move all of this logic to the web server and have it fetch the images and perform the conversion/upload process.

This function may actually work in an automation script -

function base64_encode (s)
{
  // the result/encoded string, the padding string, and the pad count
  var base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var r = ""; 
  var p = ""; 
  var c = s.length % 3;

  // add a right zero pad to make this string a multiple of 3 characters
  if (c > 0) { 
    for (; c < 3; c++) { 
      p += '='; 
      s += "\0"; 
    } 
  }

  // increment over the length of the string, three characters at a time
  for (c = 0; c < s.length; c += 3) {

    // we add newlines after every 76 output characters, according to the MIME specs
    if (c > 0 && (c / 3 * 4) % 76 == 0) { 
      r += "\r\n"; 
    }

    // these three 8-bit (ASCII) characters become one 24-bit number
    var n = (s.charCodeAt(c) << 16) + (s.charCodeAt(c+1) << 8) + s.charCodeAt(c+2);

    // this 24-bit number gets separated into four 6-bit numbers
    n = [(n >>> 18) & 63, (n >>> 12) & 63, (n >>> 6) & 63, n & 63];

    // those four 6-bit numbers are used as indices into the base64 character list
    r += base64chars[n[0]] + base64chars[n[1]] + base64chars[n[2]] + base64chars[n[3]];
  }
   // add the actual padding string, after removing the zero pad
  return r.substring(0, r.length - p.length) + p;
}

@Bill.French , Can you please explain how to do this, like how to fetch the binary value of the image from Airtable URL.

This is amazing, thank you very much.

Yes, that’s like final plan if all these doesn’t work.

If you have an image in base64 format and want it to be an attachment in Airtable, you can use an conversion service (like CloudConvert) to convert the raw text into an image file, and then upload the image file into an attachment field.

But it sounds like this is not what the OP wants to do.

It sounds like the OP’s customer will be uploading photos to an attachment field. Scripting app always needs to be run manually, so this won’t work unless you are okay with having a big delay until someone can push that button. Also, the base64 version of an image is likely too large to fit in a long text field. You would have to generate a text file with the base64 info at a public url, and then move that file to a different attachment field. That’s too complicated.

Interestingly enough, the code int the Web Dev link that Bill shared actually works in Scripting app, although the editor complains that it doesn’t know what a FileReader is. On the other hand, the exact same code doesn’t work in an automaton script. You can create the FileReader, but FileReader.readAsDataUrl is not supported.

This sounds like the best option.