feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

javascript FileReader.

Fri Sep 19, 2014 8:44 am

I want to copy the single-line text of a file ('temperature.txt') into the html page code I have displaying properly in Iceweasel and midori, written using cherrypy in python3.2. In the javascript, want, every 6 seconds, to read the contents of that file, which will be separately written to. I have

Code: Select all

setInterval("sequence();", 6000);
which calls the function 'sequence', and in the 'sequence' declaration I have

Code: Select all

function sequence() {
          var frd = new FileReader();
          frd.onload = function(event) {
                   var temptxt = event.frd.result;
                   document.getElementById('tempdata').innerHTML = temptxt;
                   };
          frd.readAsText('temperature.txt');
 
The html file has a button which, onclick, triggers 'setInterval 'amongst other things which change the browser's display. However, no cnage is seen to the .innerHTML referred to above. Using 'inspectElement' on the Firefox browser dispaly, the console then reports TypeError: Argument 1 of FileReader.readAsText is not an object. and if I remove the quotation marks around 'temperature.txt it reports ReferenceError: temperature is not defined. On Midori the report is ReferenceError: Can't find variable: (repeated xx times. The 'xx' is an integer that increases, as expected, every 6 seconds. This is the first time I have tried to use FileReader so it comes as no surprise that I've made a mess of the coding, but although I have read pages of tutorials on FileReader and have changed different bits of the code to suck it and see, I'm at a loss to sort this out. Can anyone put me right, please?
Last edited by feverish on Fri Sep 19, 2014 1:23 pm, edited 3 times in total.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: javascript FileReader.

Fri Sep 19, 2014 9:24 am

Where did you find about the 'FileReader' ? (do you have a link to the docs??)
I do script alittle and it's new to me !

To do what you want with what you have setup ( cherrypy ect )
i would think
- javascript would make a XMLHttpRequest to the (python) server
- python should read the file and send it when requested (perhaps via cgi) to the browser

I have never hear of any javscript 'FileReader' being avalable in a browser !



* i aint no expert or nothing
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: javascript FileReader.

Fri Sep 19, 2014 10:24 am

I guess this is how lots of beginners learn :-). I Googled HTML FileReader and Javascript FileReader after doing more general searches and there are many pointers from that, such as http://www.htmlgoodies.com/beyond/javas ... xvDGVPhttM. FileReader was evidently new in HTML5, but not so new to using it as me! I confess I find the web 'tutorials' generally helpful as an introduction but leave me with as many questions as answsers when I try them out, but the help so often given in here, after me putting some time in reading and narrowing down problems with actual code are a huge help to learning. So thanks for your pointer to yet another possible method, I'll happily turn to it if the way the tutorials say should work, fail to do so.
Last edited by feverish on Fri Sep 19, 2014 2:43 pm, edited 2 times in total.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: javascript FileReader.

Fri Sep 19, 2014 10:47 am

feverish wrote:I guess this is how lots of beginners learn :-).
Yep many years a go i was doing the same , sadly long before any sort of crossbrowser compatability was simple :cry: ( dark ages of the web ) the memories still haunt me , any way ...
I guess you will need to check the version of the browser's you are using have implmented that bit of the html5 spec :o ( nothing changes ! )

+
Should

Code: Select all

Function
be all lower case ? ( ther might be exceptions to that rule but i think its worth a try i just tested and Function gives criptic errors ;)
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: javascript FileReader.

Fri Sep 19, 2014 1:03 pm

You are right, thank you. Unfortunately, in 'copying' the coding from Raspi to PC, I made that mistake, now edited. The coding in Raspi has a small f.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

tbd.pi
Posts: 34
Joined: Sat Aug 11, 2012 11:21 am
Location: NJ,USA
Contact: Website

Re: javascript FileReader.

Fri Sep 19, 2014 1:13 pm

Are you expecting the user to provide the file on their system? Or do you hold the temperatures?

FileReader is an API for reading files off of the users machine. It sounds very much like you've got a file on the server that you need to read from to the clients machine every 6 seconds. Am I right?

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: javascript FileReader.

Fri Sep 19, 2014 6:04 pm

My Raspi holds all the files for this project locally. I understood that only 'local' files are readable using FileReader. Was this correct? Have you used FileReader?
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: javascript FileReader.

Fri Sep 19, 2014 6:36 pm

feverish wrote: The coding in Raspi has a small f.
Is it working now? !
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?

fordcars
Posts: 10
Joined: Thu Jun 26, 2014 6:23 pm

Re: javascript FileReader.

Sat Sep 20, 2014 5:24 am

If the file is on the server, I would have the server send it to all the connected clients each 6 seconds using Ajax or Websockets. I don't know how to do this in python, but with node.js I would use socket.io

Good luck!

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: javascript FileReader.

Sat Sep 20, 2014 7:48 am

fordcars wrote:If the file is on the server, I would have the server send it to all the connected clients each 6 seconds using Ajax or Websockets. I don't know how to do this in python, but with node.js I would use socket.io
Good luck!
With python (i guess) just make the file available , puting it i the www dir or what ever so its available for request.
Then just make the (XMLHttp)request or wot ever from the client .
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: javascript FileReader.

Sat Sep 20, 2014 9:09 am

r3d4 wrote:
feverish wrote: The coding in Raspi has a small f.
Is it working now? !
Not yet! :-) However, I think I see progress. The FileReader method apparently needs a File object and I'm currently at a loss to find where such an object exists or how to turn my text file into an object for the purpose, whereas the XMLHttprequest method looks for a filename. The latter problem seems to be it does not look in the place the file exists on my Raspi and I can't find the place it is actually looking at, despite my having also tried entering the path and filename with

Code: Select all

xmlhttp.open("GET","home/pi/temperature.txt",true);
xmlhttp.send();
,
which was shown in the 'how to' example Google threw up. midori's 127.0.0.1:8080 browser display's inspectElement console reports (every 6 seconds, as I want, that the html is looking for the file, with the result:-
GET http://127.0.0.1:8080/home/pi/temperature.txt 404 (Not Found)
so it seems to me I'm getting there but need a final push to let ajax find the file, preferably from where it now is.
Grateful for a solution.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: javascript FileReader.

Sat Sep 20, 2014 9:30 am

feverish wrote:
r3d4 wrote:
feverish wrote: The coding in Raspi has a small f.
Is it working now? !
Not yet! :-) However, I think I see progress.
8-) It looks like ur geting close to making the %$&@ thing work :!:

Re xmlHttpRequiest & file path's ,.... as you are using the cherrypi (server!) it needs to 'serve' the static text file so the client(xmlHttpRequest) can request! the file .

Try looking up 'serve static files from cherrypy' or some sutch thing .

I did look a little and apparently you can set some config options in cherrypi that might hellp like "tools.staticdir.debug = True" .

bon veine
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?

Return to “Other programming languages”