Javascript; FileSystemObject;Localhost
hi,
im using this code
Code:
<script>
function load()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
var str='Scripting.FileSystemObject';
fso = new ActiveXObject(str);
var file = 'c:/testfile.txt';
f = fso.OpenTextFile(file, ForWriting, true);
var text ='Hello world!';
f.Write(text);
f.Close();
alert('done');
}
load();
</script>
to create a file on my Hard Drive by saving the page simply in Desktop and execute it by double clicking the page
this is a simple html page.
but i want to create this file on my localhost by putting this html file in my localhost and i want to execute this file like
Code:
http://localhost/dir/file.htm
but it does not create the file on localhost with this code
Code:
<script>
function load()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
var str='Scripting.FileSystemObject';
fso = new ActiveXObject(str);
var file = 'http://localhost/dir/testfile.txt';
f = fso.OpenTextFile(file, ForWriting, true);
var text ='Hello world!';
f.Write(text);
f.Close();
alert('done');
}
load();
</script>
and shows an error like
Code:
bad file name or number
Re: Javascript; FileSystemObject;Localhost
I believe it is failing because it is expecting a path, not a URI. Have you tried using relative paths?
Re: Javascript; FileSystemObject;Localhost
im having the problem of path. I also tried Relative path like
Code:
var file = 'http://localhost/dir/testfile.txt';
but it creates the file on Desktop hahaha
i don't understand what exact path i should put on
var file
Re: Javascript; FileSystemObject;Localhost
FileSystemObject looks at exactly what it says...the system's disk. It cannot tell what localhost is since it is not part of a web server. I'm sorry, but you are going to have to specify system relative path.
Re: Javascript; FileSystemObject;Localhost
can you pls show me the correct way?
Re: Javascript; FileSystemObject;Localhost
Take the http:// off the front, that indicates a URI or URL. Just use a relative path starting from the root location of your web app.
Re: Javascript; FileSystemObject;Localhost
i tried this now
Code:
var file = '//localhost/dir/testfile.txt';
and i got an error Permission Denied
how can i get it fixed pls?
Re: Javascript; FileSystemObject;Localhost
Re: Javascript; FileSystemObject;Localhost
or im doing something wrong here in the Relative PATH.
Code:
var file = '//localhost/dir/testfile.txt';
pls correct my path to get it working
Re: Javascript; FileSystemObject;Localhost
As we have both stated many times, you cannot use localhost. You MUST use the relative path. That means c:\path\to\file.
Re: Javascript; FileSystemObject;Localhost
but i want to keep the feedback of the users in a .txt file for my webpage.
is not that possible?
Re: Javascript; FileSystemObject;Localhost
That is exactly what it is doing. For instance, let's say you're using IIS and the root for your web page is in c:\inetpub\wwwroot\myapp. You likely have a file in there called index.html (or index.php etc. etc.). When you use a *relative path*, it will start at the root directory of the web. Try to stay away from fully qualified paths (such as C:\my\path\my.file) as you will likely run into permission issues. If you specify the path "files/data.dat" then you will actually be referencing the file c:\inetpub\wwwroot\myapp\files\data.dat. I can't really define what a relative path is any clearer than that.