Page 1 of 1
Load/save file
Posted: Sun May 13, 2018 3:15 pm
by Timo
I have some JavaScript libraries, files on my server, which I use in nuBuilder. My goal is to manage these external files by means of a form.
When I open a record, an external file should be loaded in a nuBuilder textarea field. When I save the record, the content of the textarea should be saved back to the JavaScript file.
general.js.png
Re: Load/save file
Posted: Sun May 13, 2018 5:47 pm
by toms
Hi,
This is how I implemented a tiny file editor.
Create a table with these fields:
file_id - type varchar(25) - primary key
file_name - type varchar(1000)
file_content - type text
Create a Browse/Edit Form with "Fast Forms". Fields field_name (input), file_content (textarea)
files_editor.PNG
In your form's Custom Code (JS):
Code: Select all
if (nuFormType() == 'edit') {
var c = atob(fileGetContents());
// instead of atob: use this class to properly decode: https://www.coditty.com/code/utf-base64-encode-in-php-and-decode-in-javascript
$('#file_content').val(c).change();
$('#file_content')
.dblclick(function() {
nuOpenAce('Javascript', this.id);
});
nuHasNotBeenEdited();
}
PHP (BE):
Code: Select all
$c = base64_encode(file_get_contents("#file_name#"));
$j = "
function fileGetContents(){
return '$c';
}
";
nuAddJavascript($j);
PHP (AS):
Code: Select all
$filename = "#file_name#";
if ($filename != '')
{
file_put_contents($filename,stripslashes("#file_content#"));
}
Whenever a record is saved, the content of file_content is saved to a file that is specified in the field file_name. When a record is opened, the file contents is loaded from the file too.
Hope that helps!
Re: Load/save file
Posted: Thu May 17, 2018 9:27 am
by Timo
Thank you toms. I'm gonna take a look at it .
Re: Load/save file
Posted: Thu May 17, 2018 9:36 am
by admin
.