Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    6
    Thanked 0 Times in 0 Posts

    JavaScript Read external file

    Hi,

    i have a question, is JavaScript can read an external file? i have an ear file, can it read external file without the external file compile together in the ear file? can we do so?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,986
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser. And for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

    But an .ear file is a Java module. Do you perhaps mean Java? Even if Javascript could read it, it could not process it.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 02-21-2012 at 11:37 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    shunaqie_89 (02-22-2012)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    yes, you can load a file from the user's pc via file input, from your server using ajax, or from anywhere using cors or jsonp.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    shunaqie_89 (02-22-2012)

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by Philip M View Post
    JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser. And for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.
    This makes it sound like you are saying no, which is wrong.
    I don't typically answer simple inquiries, but I believe it a disservice to OPs and lurkers to dismiss such basic questions.

    File inputs and drag and drop access files on the "client's operating system", ajax, forms, <script> and Image() all "communicate with the server", registerPrototcolHandler() changes the "default behaviour of the browser" in terms of url/file handling, and the "same origin policy" has been outmoded for several browsers versions by now. Even in old browsers, jsonp and form POSTs go "beyond the domain of the current page". None of your points stand up in this day and age.


    And let's not forget that the js in node.js stands for javascript; you tellin me javascript can't use files? really? Come on, give me a break; it's 2012 already...

    Just because one may not know about certain technologies and techniques doesn't mean they don't exist. Now you know, so stop posting this garbage from years ago when i joined. Even with an edit or two, it's completely mis-leading on it's face. I will rebuke this malarkey 100% of the time i see such lies and rubbish propagated.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • The Following 2 Users Say Thank You to rnd me For This Useful Post:

    blaze4218 (02-21-2012), shunaqie_89 (02-22-2012)

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    JavaScript can read files when run on the client computer provided that the script is not running in a browser. One such place where you can easily set up client side JavaScript that can read files in in Adobe Air which has JavaScript as one of the three languages that it supports for writing applications.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    shunaqie_89 (02-22-2012)

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by felgall View Post
    JavaScript can read files when run on the client computer provided that the script is not running in a browser.
    i beg to differ.

    1. go to http://danml.com/sandbox/
    2. un-maximize your browser window
    3. drag any text-ish file from a folder and drop it into the large white box that comprises the lower half of the screen.


    You can also drag into the IN, DATA, AND HTML tab's boxes, but not LOG or CODE...


    It seems to work for me in chrome and firefox, which are both considered browsers.

    I see the file contents in the <textarea>, so if JavaScript can't "read files", I must be a witch instead of a programmer.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    shunaqie_89 (02-22-2012)

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    I see the file contents in the <textarea>, so if JavaScript can't "read files", I must be a witch instead of a programmer.
    On that page JavaScript isn't reading the file. The file is being loaded to the server and read by a server side script whick then passes the content back to JavaScript in the response to an Ajax call.

    JavaScript running in the browser does not have any commands for reading or writing files.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    shunaqie_89 (02-22-2012)

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by felgall View Post
    On that page JavaScript isn't reading the file. The file is being loaded to the server and read by a server side script whick then passes the content back to JavaScript in the response to an Ajax call.
    nope. no ajax. no server-side code in use.
    check firebug again - no net tab activity.

    I gotcha, but with good news, so rejoice.

    This is code and as such, things are easy to prove; you don't have to take my word for it.

    Here is an html file that can be served via http which contains a textarea into which you can drag and drop a file:


    Code:
    <html>   <textarea cols="120" rows="40"></textarea>
    <script>
    
    function handleDrop(e){
    	e.stopPropagation();
    	e.preventDefault();
    
    	var elm=e.target||e.srcElement,
    	 files=e.dataTransfer.files, 
    	 file=files[0],
    	 reader=new FileReader();
    
    	reader.onload= function(event){  
    		elm.value=event.target.result||elm.value; 
    		document.title="editing: "+file.name;
    	};//end reader.onload()
    
    	reader.readAsBinaryString(file);
    
    }//end handleDrop()
    
    function handleDragOver(e){
    	e.stopPropagation();
    	e.preventDefault();
    }//end handleDragOver()
        
    var myElm=document.getElementsByTagName("textarea")[0];
      myElm.addEventListener('dragover', handleDragOver, false);
      myElm.addEventListener('drop',     handleDrop,     false);
    </script>
    </html>
    Last edited by rnd me; 02-22-2012 at 03:47 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    nope. no ajax. no server-side code in use.
    Well there is in that page but obviously (now) for some other purpose.

    I hadn't come across the File API for JavaScript before (not surprising as the preliminary draft for it is dated October 2011). Probably be a while before all browsers actually support it but since it relies on user interaction to allow the file to be read it gets around the security issues associated with a web site being able to read local files.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts
    Yes javascript can be used to read external files.
    You do not need AJAX/JSON if the files are on the same server.
    Simply use an iframe to read the file contents and parse as needed.

    You can find a short working cross browser example here:
    Read Local Text Files, Quick Set Properties, and Custom Error Handler

    Download the zip file found there (readText.zip), unzip to your server and run the example.

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rdspoons View Post
    Yes javascript can be used to read external files.
    You do not need AJAX/JSON if the files are on the same server.
    Using an iframe like that is called Ajax. JavaScript has four different ways it can use Ajax - XMLHttpRequest, or one of the three tags - object, img or script (iframe is just an obsolete variant of object and all iframes were supposed to be replaced with object tags when HTML 4 became a standard in 1997). All four have advantages and disadvantages - for example script is the only one of the four that avoids the same domain restriction, XMLHttpRequest is the only one to trigger an event when the data is received, img is useful for passing data to the server when no response is required, object/iframe is the most limited and useless of the four.

    Anyway JavaScript can't read any files from the server using any of the Ajax techniques as whichever of the four ways you write your Ajax you still need a server side script to actually read the file and convert it to a form that JavaScript can use.

    It does appear though that JavaScript running in the browser can read local files that are on the same computer as the browser provided that the browser owner initiates the read request.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,986
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by rnd me View Post
    Just because one may not know about certain technologies and techniques doesn't mean they don't exist. Now you know, so stop posting this garbage from years ago when i joined. Even with an edit or two, it's completely mis-leading on it's face. I will rebuke this malarkey 100% of the time i see such lies and rubbish propagated.
    Sorry, rndme. No need to start blowing a fuse. I phrased my response carefully and I stand by what I wrote. JavaScript running in the browser does not have any commands for reading or writing files on the server. That was the question asked by the OP. An iframe is simple HTML and involves no Javascript.
    Last edited by Philip M; 02-22-2012 at 08:11 AM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    JavaScript running in the browser does not have any commands for reading or writing files
    You mean apart from the recently added FileReader() object that can read files when the browser owner requests it. Currently so new that only Firefox and Chrome support it.
    Last edited by felgall; 02-22-2012 at 08:02 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,986
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by felgall View Post
    You mean apart from the recently added FileReader() object that can read files when the browser owner requests it. Currently so new that only Firefox and Chrome support it.
    Yes, see - http://www.html5rocks.com/en/tutorials/file/dndfiles/

    But something that works only in HTML5 and then not in IE is in my view of no practical use or value. It will be a few years before HTML4 browsers are obsolete.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    Yes, see - http://www.html5rocks.com/en/tutorials/file/dndfiles/

    But something that works only in HTML5 and then not in IE is in my view of no practical use or value. It will be a few years before HTML4 browsers are obsolete.
    It is JavaScript - NOT HTML 5. So it should be available in all browsers within the next few years - there's no reason why it wouldn't work with HTML 1 provided the browser supports the file.api. You will not have to wait 20 years (or more) for HTML 5 to be finished to be able to use it. Just because HTML 5 is now being put forward as a generic term for all the new additions to HTML, CSS, JavaScript, fast food, racing cars, egg plants, monkeys, and moon rockets doesn't mean that the term has any relevant meaning beyond HTML itself. It has as little meaning to JavaScript as it does to moon rockets - perhaps less.

    This suddenly using a term for thousands of things that have nothing to do with what it was originally specific to is kind of like the way Web 2 suddenly got used for lots of things that have nothing to do with JavaScript/Ajax and Web 3 is sometimes used for things way beyond generic database tables.

    That this isn't yet officially a part of JavaScript the way things like postMessage and "use strict"; are is why I am not rushing to add it to my "by example" site - I found it in David Flanagan's "JavaScript: the Definitive guide" now that I found out that it exists - had obviously ignored it before because it is still only part way through the draft process before becoming a standard (although much closer that HTML 5 is).
    Last edited by felgall; 02-22-2012 at 09:13 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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