Custom Module (node) - Convert file to base 64

Introducing @hyperbytes/wappler-file-to-base64-with-save-option

I have noticed recently some users asking about converting files to base64 encoding for use in API's.

While Sid has previously produced an extension for something like this I have produced a variation on this idea which allows the file to not only be converted and output but also to be saved.
This uses no additional external libraries

image

This extension takes a file path and returns the file as a base64 encoded string.

Also, optionally (set by checkbox), the file can be written back in it's base64 form to a path for subsequent use.

Not extensively tested but seems to be working perfectly on my system.

Any issues, let me know or any suggestions for improvement welcome

9 Likes

What do you think of the idea of directly accept a file variable instead of a file path? Maybe in some cases one doesn't need to save the file to disk

I can look at it although at this moment i dont have a clue how to do that :smirk:

It's just a preparation for the future, I don't personally need it :slight_smile:

Even Wappler steps like CSV handling are so bizarre in the sense they need a file path to import. One can't import directly from an incoming POST request, you have to write to a file first...

You mean, for example, direct from a $_POST variable before any upload?

Not tried but i assume the current module could use upload.path as a file source if using that.

I'm under the assumption file uploads will be accessible in a $_POST variable as an array of bytes somehow, don't know how true is that in Wappler context

I don't think so, the data is not actually part of the $_POST variable, the actual file is, i believe, uploaded to the server's temporary folder and would need to be accessed via the $_FILES superglobal

Accessing form POST directly is looking problematic. If it had been a quick fix i would have done it but conscious i am getting distracted from my main projects of Customs extensions series and integration of Onesignal.
May come back to it later.

Rome wasn't built in a day Brian.

Just ask Biggus!

:slight_smile:

1 Like