469,328 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Files

Hi everybody...I would like to know if it is possible and how to work
with files in javascript.

Thanks.

**Alan**
Jul 23 '05 #1
2 1416


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/
Jul 23 '05 #2
"Giacomin Alan" <g.******@virgilio.it> wrote in message
news:79**************************@posting.google.c om...
Hi everybody...I would like to know if it is possible and how to work
with files in javascript.


If the script host which is hosting your ECMAScript provides an object
that lets you manipulate the file system, then you can manipulate the
file system using JavaScript/JScript/ECMAScript.

Microsoft provides such an object. It can be used from script running in
an HTA, script running in HTML if that HTML is hosted on your local hard
disk (and you agree to let active content run in Windows XP Service Pack
2) and the Windows Script Host.

It is also available in IIS (which is also a script host) and can be
accessed from JScript there.

The object Microsoft uses is called the FileSystemObject (FSO for
short):

<url:
http://msdn.microsoft.com/library/en...FileSystem.asp
/>

It appears appears Gecko-based browsers (or at least Mozilla and
Firefox) can do this as well, although I am unsure of the restrictions
governing their ability to actually create and manipulate files
(obviously script loaded from the Internet can not do these things).
Creating a new file in Mozilla appears to involve something along the
lines of:

var newFile =
Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile );

This seems like a good place to start if you want to learn more: <url:
http://www.mozilla.org/scriptable/co...ts_object.html />

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.