Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2012, 07:48 AM   PM User | #1
shunaqie_89
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 6
Thanked 0 Times in 0 Posts
shunaqie_89 is an unknown quantity at this point
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?
shunaqie_89 is offline   Reply With Quote
Old 02-21-2012, 11:35 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 02-21-2012 at 11:37 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
shunaqie_89 (02-22-2012)
Old 02-21-2012, 05:16 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
shunaqie_89 (02-22-2012)
Old 02-21-2012, 05:27 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
The Following 2 Users Say Thank You to rnd me For This Useful Post:
blaze4218 (02-21-2012), shunaqie_89 (02-22-2012)
Old 02-21-2012, 08:25 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
shunaqie_89 (02-22-2012)
Old 02-21-2012, 08:40 PM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
shunaqie_89 (02-22-2012)
Old 02-22-2012, 01:49 AM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
shunaqie_89 (02-22-2012)
Old 02-22-2012, 03:31 AM   PM User | #8
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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>
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 02-22-2012 at 03:47 AM..
rnd me is offline   Reply With Quote
Old 02-22-2012, 05:54 AM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 02-22-2012, 07:08 AM   PM User | #10
rdspoons
New Coder

 
Join Date: Jun 2009
Posts: 81
Thanks: 0
Thanked 8 Times in 8 Posts
rdspoons is on a distinguished road
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:
http://www.codingforums.com/showthread.php?t=203024

Download the zip file found there (readText.zip), unzip to your server and run the example.
rdspoons is offline   Reply With Quote
Old 02-22-2012, 07:35 AM   PM User | #11
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 02-22-2012, 07:37 AM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 02-22-2012 at 08:11 AM.. Reason: Typo
Philip M is offline   Reply With Quote
Old 02-22-2012, 07:56 AM   PM User | #13
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 02-22-2012 at 08:02 AM..
felgall is offline   Reply With Quote
Old 02-22-2012, 08:06 AM   PM User | #14
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 02-22-2012, 09:00 AM   PM User | #15
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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).
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 02-22-2012 at 09:13 AM..
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:56 AM.


Advertisement
Log in to turn off these ads.