CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2007
    Posts
    445

    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

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  2. #2
    Join Date
    Aug 2005
    Location
    Milwaukee, WI
    Posts
    55

    Re: Javascript; FileSystemObject;Localhost

    I believe it is failing because it is expecting a path, not a URI. Have you tried using relative paths?
    Meddle not in the affairs of dragons, human, for thou art crunchy and taste good with ketchup.

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

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    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.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Aug 2007
    Posts
    445

    Re: Javascript; FileSystemObject;Localhost

    can you pls show me the correct way?

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  6. #6
    Join Date
    Aug 2005
    Location
    Milwaukee, WI
    Posts
    55

    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.
    Meddle not in the affairs of dragons, human, for thou art crunchy and taste good with ketchup.

  7. #7
    Join Date
    Aug 2007
    Posts
    445

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

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  8. #8
    Join Date
    Aug 2007
    Posts
    445

    Unhappy Re: Javascript; FileSystemObject;Localhost

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

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

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

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    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.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

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

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  12. #12
    Join Date
    Aug 2005
    Location
    Milwaukee, WI
    Posts
    55

    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.
    Meddle not in the affairs of dragons, human, for thou art crunchy and taste good with ketchup.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured