PDA

View Full Version : Read, write and edit a text file


shy2
02-06-2008, 01:00 AM
Hello everyone,

Does anybody of you know how to READ, WRITE & CREATE a text file?
I've been searching about this for a while but it seems that openlaszlo cannot do it. I was hoping there is a filesystem functionality here... please help.

Thanks,
Shy2

kmeixner
02-12-2008, 12:46 PM
OpenLaszlo is based on JavaScript and Flash which for security reasons have limited access to the web user's local filesystem so there is no way to read/write a file directly from to/from the users file system. You could however send/receive data to/from a server-side script written in your favourite server-side scripting language (PHP, ASP, JSP, CGI, etc.) that would save/read this data to the server-side file system in a file.

<class name="myclass">

<attribute name="filename" type="string" value="myfile.txt" />
<attribute name="contents" type="string" value="This sentence is the contents of my file." />

<!--- @keywords private -->
<!--- Holds Temporarily created save listener -->
<attribute name="callbackDelegateHolder" />

<dataset name="myfileoperator" src="http://www.mysite.com/fileoperator.php" />

<!-- SAVE FILE CONTENTS -->

<method name="saveFile">
var d=this.myfileoperator;
var p=new LzParam();
p.addValue("filename", this.filename);
p.addValue("contents", this.contents);
p.addValue("action", "save");
d.setQueryString(p);

Debug.write(d.getSrc()+'?'+d.getQueryString()); // displays posted URL to debugger window in debug mode

// sets up listener that will call saveFileCallback() when data is received to 'myfileoperator' dataset:
this.callbackDelegateHolder = new LzDelegate(this, 'saveFileCallback', this.myfileoperator, "ondata");

d.doRequest(); // execute request

// Continues with saveFileCallback() below upon 'ondata' event thrown by 'myfileoperator'
</method>
<!--- @keywords private -->
<!--- Called by save listener upon receipt of data after a save request to myfileoperator -->
<method name="saveFileCallback">
this.callbackDelegateHolder.unregisterAll(); // destroys delegate

var dp = this.myfileoperator.getPointer(); // get datapointer
var successful = dp.xpathQuery('result/successful/text()');
var message = dp.xpathQuery('result/message/text()');

Debug.write('Save Successful: '+successful);
Debug.write('Message: '+message);

</method>


<!-- READ FILE CONTENTS -->

<method name="readFile">
var d=this.myfileoperator;
var p=new LzParam();
p.addValue("filename", this.filename);
p.addValue("action", "read");
d.setQueryString(p);

Debug.write(d.getSrc()+'?'+d.getQueryString()); // displays posted URL to debugger window in debug mode

// sets up listener that will call readFileCallback() when data is received to 'myfileoperator' dataset:
this.callbackDelegateHolder = new LzDelegate(this, 'readFileCallback', this.myfileoperator, "ondata");

d.doRequest(); // execute request

// Continues with saveFileCallback() below upon 'ondata' event thrown by 'myfileoperator'
</method>
<!--- @keywords private -->
<!--- Called by save listener upon receipt of data after a save request to myfileoperator -->
<method name="readFileCallback">
this.callbackDelegateHolder.unregisterAll(); // destroys delegate

var dp = this.myfileoperator.getPointer(); // get datapointer
var successful = dp.xpathQuery('result/successful/text()');
var message = dp.xpathQuery('result/message/text()');
var contents = dp.xpathQuery('result/contents/text()');

Debug.write('Save Successful: '+successful);
Debug.write('Message: '+message);
Debug.write('File Contents: '+contents);

</method>

</class>

The http://www.mysite.com/fileoperator.php residing on the server-side would be a PHP script. It would take and return
the following:

1) SAVE:

Accepts posted params as in this example:

http://www.mysite.com/fileoperator.php?filename=myfile.txt&contents=This%20sentence%20is%20the%20contents%20o f%20my%20file.&action=save

Saves the contents of the 'contents' param in a file named with the name in the 'filename' param and return
an XML message as follows:

<result>
<successful>true</successful>
<message>The file was saved successfully.</message>
</result>

2) READ:

Accepts posted params as in this example:

http://www.mysite.com/fileoperator.php?filename=myfile.txt&action=read

Reads the contents of the file named with the name in the 'filename' param and returns
an XML message with the file contents as follows:

<result>
<successful>true</successful>
<message>The file was read successfully.</message>
<contents>This sentence is the contents of my file.</contents>
</result>

d~l
02-12-2008, 02:41 PM
you can also create, read, write a "shared object" (Flash 8+ only) which is persistent .. but persistent data might be lost if shared objects are cleared .. search for "shared object"