473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1558


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
18
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now...
18
by: UJ | last post by:
Folks, We provide custom content for our customers. Currently we put the files on our server and people have a program we provide that will download the files. These files are usually SWF, HTML or...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.