Giacomin Alan wrote:
I would like to know if it is possible and how to work
with files in javascript.
It depends on the environment you are using JavaScript in whether it is
possible and if so how it is possible.
For instance on Windows with J(ava)Script in a HTA, an ASP page or a
Windows Script Host file it is possible and there you can use the
Scripting.FileSystemObject to read and write text files:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/FSOoriFileSystemObject.asp>
With Opera (at least when tested here with Opera 7 and Sun Java 1.5)
when Java it is enabled you can use LiveConnect in pages loaded locally
(file: URL) to read and write files e.g. try
var path = location.pathname;
var dir = path.substring(0, path.lastIndexOf('/') + 1);
dir = dir.replace(/%20/g, ' ');
var newFileName = dir + 'test2005011701.txt';
if (navigator.javaEnabled() && typeof java != 'undefined') {
var fileWriter = new java.io.FileWriter(newFileName);
fileWriter.write("Kibology for all.\r\nAll for Kibology.\r\n");
fileWriter.write("Written at " + new Date());
fileWriter.close();
window.open('file://' + newFileName);
}
Of course access to the local file system that way is not allowed when
the page with the script is loaded from a http: URL.
If you are asking about script in a HTML page usually loaded via HTTP in
a browser then for security reasons such a script is not allowed file
access to the local file system. In some browsers (IE/Win, Mozilla,
Safari 1.2, Opera 7.60 preview and 8.00 beta) you can however load XML
from the server, manipulate it and send it back to the server:
<http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616>
<http://jibbering.com/2002/4/httprequest.html>
--
Martin Honnen
http://JavaScript.FAQTs.com/