Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Giacomin Alan

    Files

    Hi everybody...I would like to know if it is possible and how to work
    with files in javascript.

    Thanks.

    **Alan**
  • Martin Honnen

    #2
    Re: Files



    Giacomin Alan wrote:
    [color=blue]
    > I would like to know if it is possible and how to work
    > with files in javascript.[/color]

    It depends on the environment you are using JavaScript in whether it is
    possible and if so how it is possible.
    For instance on Windows with J(ava)Script in a HTA, an ASP page or a
    Windows Script Host file it is possible and there you can use the
    Scripting.FileS ystemObject to read and write text files:
    <http://msdn.microsoft. com/library/default.asp?url =/library/en-us/script56/html/FSOoriFileSyste mObject.asp>

    With Opera (at least when tested here with Opera 7 and Sun Java 1.5)
    when Java it is enabled you can use LiveConnect in pages loaded locally
    (file: URL) to read and write files e.g. try

    var path = location.pathna me;
    var dir = path.substring( 0, path.lastIndexO f('/') + 1);
    dir = dir.replace(/%20/g, ' ');
    var newFileName = dir + 'test2005011701 .txt';

    if (navigator.java Enabled() && typeof java != 'undefined') {
    var fileWriter = new java.io.FileWri ter(newFileName );
    fileWriter.writ e("Kibology for all.\r\nAll for Kibology.\r\n") ;
    fileWriter.writ e("Written at " + new Date());
    fileWriter.clos e();
    window.open('fi le://' + newFileName);
    }

    Of course access to the local file system that way is not allowed when
    the page with the script is loaded from a http: URL.


    If you are asking about script in a HTML page usually loaded via HTTP in
    a browser then for security reasons such a script is not allowed file
    access to the local file system. In some browsers (IE/Win, Mozilla,
    Safari 1.2, Opera 7.60 preview and 8.00 beta) you can however load XML
    from the server, manipulate it and send it back to the server:
    <http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616>
    <http://jibbering.com/2002/4/httprequest.htm l>

    --

    Martin Honnen

    Comment

    • Grant Wagner

      #3
      Re: Files

      "Giacomin Alan" <g.alan86@virgi lio.it> wrote in message
      news:79197cc5.0 501170810.59670 05b@posting.goo gle.com...[color=blue]
      > Hi everybody...I would like to know if it is possible and how to work
      > with files in javascript.[/color]

      If the script host which is hosting your ECMAScript provides an object
      that lets you manipulate the file system, then you can manipulate the
      file system using JavaScript/JScript/ECMAScript.

      Microsoft provides such an object. It can be used from script running in
      an HTA, script running in HTML if that HTML is hosted on your local hard
      disk (and you agree to let active content run in Windows XP Service Pack
      2) and the Windows Script Host.

      It is also available in IIS (which is also a script host) and can be
      accessed from JScript there.

      The object Microsoft uses is called the FileSystemObjec t (FSO for
      short):

      <url:
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

      />

      It appears appears Gecko-based browsers (or at least Mozilla and
      Firefox) can do this as well, although I am unsure of the restrictions
      governing their ability to actually create and manipulate files
      (obviously script loaded from the Internet can not do these things).
      Creating a new file in Mozilla appears to involve something along the
      lines of:

      var newFile =
      Components.clas ses['@mozilla.org/file/local;1'].createInstance (Components.int erfaces.nsILoca lFile);

      This seems like a good place to start if you want to learn more: <url:
      http://www.mozilla.org/scriptable/co...ts_object.html />

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq


      Comment

      Working...