#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14

    Reading file contents in JavaScript


    Hi all, time for another question.

    I've been toying around unsuccessfully with the IE run time scripting engine's file system object, and there's got to be an easier way to do this.

    I have an html document that needs to do the following:

    1) Get a filename from the user.
    2) Read the file contents.
    3) Return the file contents (window.returnValue = contents). The window was originally created with window.showModalDialog().
    4) Close the window.

    Is it possible to somehow, using an <INPUT type=file> element, and posting to a javascript function, to read the file contents? I can't post to any cgi processes, because the page is being displayed in a program I wrote, and obviously, I want all the data handled internally (no posting to any internet sites).

    -Mike
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    Nope, security issues stop you accessing the file system.

    If the page is displayed in a program though, ie something that uses the IE COM and is written in VB, then you could pass the file value to the underlying program to deal with. Somehow, I'm sure I did this a long time ago.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Yeah, that's what I thought I'd had to do. I *did* get the script to read the file through something like this.

    Code:
    var fso, f;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f = fso.OpenTextFile("c:\test.txt", 1);
    window.returnValue = f.ReadAll();
    f.Close();
    window.close();
    Problem is, that brings up a security warning (and rightfully so!). Just wondering if there was a way to supress the warning, since the file is handled locally, like some way to let the browser know that the page is a "trusted site."

    Ah well, passing the filename to the underlying program for processing isn't such a big deal. Pain in the butt, but not a big problem.

    -Mike
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    vb script, I suppose you can make the site 'trusted' but is it worth the hassle?

IMN logo majestic logo threadwatch logo seochat tools logo