Click to See Complete Forum and Search --> : Javascript; FileSystemObject;Localhost


chunks
January 30th, 2009, 11:26 PM
hi,
im using this 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

http://localhost/dir/file.htm


but it does not create the file on localhost with this 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

bad file name or number

gitter1226
January 31st, 2009, 12:05 AM
I believe it is failing because it is expecting a path, not a URI. Have you tried using relative paths?

chunks
January 31st, 2009, 12:14 AM
im having the problem of path. I also tried Relative path like


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

PeejAvery
January 31st, 2009, 10:45 AM
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.

chunks
January 31st, 2009, 01:57 PM
can you pls show me the correct way?

gitter1226
January 31st, 2009, 03:34 PM
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.

chunks
February 1st, 2009, 12:42 PM
i tried this now


var file = '//localhost/dir/testfile.txt';



and i got an error Permission Denied

how can i get it fixed pls?

chunks
February 1st, 2009, 12:50 PM
...

chunks
February 1st, 2009, 12:51 PM
or im doing something wrong here in the Relative PATH.

var file = '//localhost/dir/testfile.txt';


pls correct my path to get it working

PeejAvery
February 1st, 2009, 01:25 PM
As we have both stated many times, you cannot use localhost. You MUST use the relative path. That means c:\path\to\file.

chunks
February 1st, 2009, 01:48 PM
but i want to keep the feedback of the users in a .txt file for my webpage.
is not that possible?

gitter1226
February 1st, 2009, 07:11 PM
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.