Server Script - Save a text in a .txt file

Hello,

Does anyone know how to save a file .txt in the database from a text content ?

This is for Coconut.co which needs an attached .config file.

save%20txt%20file

Bubble has a server side node.js environment that you can access via custom plugin or the Toolbox plugin.

Then: https://ourcodeworld.com/articles/read/297/how-to-create-a-file-using-the-filesystem-fs-module-in-node-js

About saving it back to database, I still have not touched that part of Bubble yet. I’ll leave it for someone else to explain :slight_smile:

1 Like

Thanks @vini_brito

I create a demo on a public app :
https://bubble.io/page?type=page&name=1_create_txt_file&id=nocodepublic&tab=tabs-1

I tried different way. The NodeJS one seems the best, but I don’t know why I get an empty result.

2 Likes

If this is a file that’s supposed to live in your site’s top level directory or similar, note that there is an area in Settings to do that.

Which area in Settings do you mean ? I don’t understand.

There’s a place to add files to your site. I’m on mobile and can’t recall exactly where that is. General perhaps?

I see. But I don’t need to host the text file in a top directory of my app.
I want to create a file and save it within a Thing’s File in the ddb such as :
image

Did you ever figure this out? I too would like to know how to programmatically create and save a file

no I did ot.
What is your use case ?

Need to programmatically create and host a text file. I figured out how to create a text file in the user’s browser using BrowserFS per @vini_brito’s tip however there is currently no way to return it back to bubble… unless perhaps if @mishav can tweak the toolbox plugin to return / upload text files.

I’ve noticed quite a few people in the forums have tried (and all failed) to do this.

Hi @zelus_pudding, if you can convert that file to Base64, then you’ll get a step closer to saving it into Bubble database. That’s how files are transfered around.

Try searching the web for a way to encode it as base64, if you do, I can probably save it to Bubble for you via some plugin functions. I found out how to save things to Bubble and implemented that in a plugin of mine.

Search terms like “encoding a file from browserify filesystem X as base64”, note that the library offers several FileSystem backends.

2 Likes

Oh, wow. I’ll try that. Thanks :slight_smile:

I’m using Base64 conversion and it generated a gigantic string. The database even crashes when entering app data and an error message appears (must have a string size limit), at this point I only have one line of data to test.
Even so, I can send the Base64 string through the api. But I think if inserting new data the database should not support it.

On the page, to check if the Base64 was saved in the database takes almost 2 minutes.

I thought about saving the Base64 string in a txt file and later accessing that file and getting its content to send through the api.

Does anyone know how to do this?

create API endpoint to https://yoururl.com/fileupload in API connector with body params

  • contents (base64 value)
  • name (filename)

Then in workflow

  • encode text to base64
  • result of this upload to bubble api endpoint

:white_check_mark: