Javascript; FileSystemObject;Localhost
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Aug 2007
Posts
445

## Javascript; FileSystemObject;Localhost

hi,
im using this code
Code:
<script>
{
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();

}
</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>
{
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();

}
</script>
and shows an error like
Code:
bad file name or number

2. ## Re: Javascript; FileSystemObject;Localhost

I believe it is failing because it is expecting a path, not a URI. Have you tried using relative paths?

3. Member
Join Date
Aug 2007
Posts
445

## 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

4. ## 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.

5. Member
Join Date
Aug 2007
Posts
445

## Re: Javascript; FileSystemObject;Localhost

can you pls show me the correct way?

6. ## 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.

7. Member
Join Date
Aug 2007
Posts
445

## 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?

8. Member
Join Date
Aug 2007
Posts
445

## Re: Javascript; FileSystemObject;Localhost

...
Last edited by chunks; February 1st, 2009 at 11:53 AM. Reason: Sorry Posted 2 Times So edited

9. Member
Join Date
Aug 2007
Posts
445

## 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

10. ## 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.

11. Member
Join Date
Aug 2007
Posts
445

## 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?

12. ## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•