Source language: Translate to:

Reading and writing local files

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

Reading and writing local files

Postby Wrangler » Sat Jul 04, 2015 10:36 am

I need to be able to read and write to a local delimited text file. When read, I will need to parse the line. That looks easy enough. But I can't seem to get started. Could someone explain how to use getitem? Is this what is used to read data from a file? Or is this even what I'm looking for? Or is it still under development? How does setitem work?

Sorry. I'm lost. Just a little push in the right direction is all I need.

And by the way: Happy Independance Day to all the Americans out there!
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1505
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: Reading and writing local files

Postby Neosoft Support » Sat Jul 04, 2015 11:03 am

Reading local files is more complicated with web/mobile apps than with Windows. There will be actions added to NeoAppBuilder to assist with this, but for now you can do it with JavaScript for web apps or PhoneGap for mobile apps.

JavaScript: http://stackoverflow.com/questions/27522979/read-a-local-textfile-using-javascript

PhoneGap: http://docs.phonegap.com/en/edge/cordova_file_file.md.html

Note: when you compile a mobile app, the PhoneGap library is automatically included with your app.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Re: Reading and writing local files

Postby Wrangler » Sat Jul 04, 2015 11:15 am

Thanks, Dave. But I'm REALLY lost. Where in neoappbuilder do I put the javascript? I get errors when I paste it into the code section. Unknown commands.
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1505
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: Reading and writing local files

Postby smokinbanger » Sat Jul 04, 2015 2:57 pm

I think you ad it as a javascript sub under the menu Project> Subroutines... Then click the new sub button then select javascript. This whole NAB thing is new to me too so this is just a guess.
Just Sayin'...
smokinbanger
 
Posts: 198
Joined: Mon Jan 16, 2012 9:53 am
Location: United States

Re: Reading and writing local files

Postby Gaev » Sun Jul 05, 2015 7:21 am

Wrangler:

For now (or even later) forget trying to read/write local files ... even if/when you can use Phonegap, you run into the issue of testing Phonegap assisted functionality on your Design/Development platform.

For what you want to accomplish, try the "Local Storage" facilities ... yes, SetItem and GetItem are what i am talking about ... try this in Button1's click section ...
Code: Select all
SetItem "userFirstName" "Donald"

... and this in Button2's click section ...
Code: Select all
GetItem "userFirstName" [neovarFirstName]
AlertBox "Local Storage" "Retrieved 'userFirstName' ... [neovarFirstName]" ""

That should give you an idea about storing/retrieving "items" in Local Storage (think cookies) ... for more information, see ... http://www.w3schools.com/html/html5_webstorage.asp.

Of course, you can store something like this ...
Code: Select all
SetItem "myCSVString" "Donald,Trump,04-01-1930,GOP"
... and after you retrieve it, you can separate the content like any other delimited string value.

If you wish to store multiple lines of delimited values, you can ...

- either use a different Item Name for each line ...
Code: Select all
SetItem "myCSVString1" "Donald,Trump,04-01-1930,GOP"
SetItem "myCSVString2" "Hillary,Clinton,08-02-1960,DEM"


- or use some other made up delimiter for each line ...
Code: Select all
SetItem "myCSVStringAllLines" "Donald,Trump,04-01-1930,GOP$$NewLine$$Hillary,Clinton,08-02-1960,DEM"
... and parse the retrieved value first on the line delimiter ($$NewLine$$) ... and then parse each parsed line for delimited fields/values.
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Reading and writing local files

Postby PaulTomo » Sun Jul 05, 2015 8:07 am

Wrangler

First of all, if you do wish to use some JS actions, you can easily do this within NeoAppBuilder by selecting Advanced from Actions and then drag beginJS to the code area of an object.

e.g. If you want to make a button show a JS alert when clicked, this is all that needs to be added.
Code: Select all
BeginJS
alert("I am a JS Alert Box!");
EndJS


I have been doing some testing with reading both files and previously created and working SQLite databases directly from local device storage and it does have some limitations and quirks depending on what you wish to do plus getting the JS code examples found on various forums to actually work is very hit and miss. But if you do go down that route and suss it out, please post a working example as it would be usful for us all.

Like Gaev mentioned, there is the NeoAppBuilder Local Storage Set/GetItem action and this will work with locally stored text but only if as explained you first of all create what you wish to store locally within the app, then using SetItem it will create a local file for you, it adds it to the local private sql db that, on Andriod as an example, can only be accessed by that app. You can actually see what gets created by running a test app in Chrome, pressing F12 to see the DeBugger and expanding LocalStorage in Resourses, then the file name plus the text content you created with SetItem are all shown as listed. These files can of course then be loaded into the app again as and when needed with GetItem.

Reading text files in direct from the web works well as an alternative but does need an internet connection but I did find this useful to use to get me started so something did actually work as I was expecting it to for something created outside the app with lots of data that I needed to be able to use within the app and I could then actually display all or some of the text on screen a needed.

Here is a sample that should work or do something similar to what you may be wanting to try out for loading in a text file with multiple lines. This enables NeoAppBuilder to read in each line from a text file saved on an ftp site or the internet and then place each line into a NeoAppBuilder array named [TestArray] by using JS for use within the app.
Code: Select all
BeginJS
 var req = new XMLHttpRequest();
  req.open('GET', 'http://mydomain/Downloads/Test.txt', false);
  req.send(null);
  if (req.readyState==4);
  $rootScope.TestVar = req.responseText;
  $rootScope.TestArray = $rootScope.TestVar.split("\r\n");
EndJS


I have been testing a combination of the both, by reading in the file from the internet and then using SetItem to store it locally for offline use, so in the example I would not need to split [TestVar] when loaded, as I could do that later once I had stored it locally.
Code: Select all
SetItem "TestData" [TestVar]

Anyway, this works for me for now or until NeoAppBuilder has this type of feature built in.

I'm no expert and am still learning as I go along too, so this may not be 100% correct!

Not sure if this helps or not but good luck.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK

Re: Reading and writing local files

Postby Wrangler » Sun Jul 05, 2015 11:26 am

Thank you, all! Now I've got something to play with. I found that the first rule to adhere to is that variables are case sensitive. So for other newbies out there, before you spend hours trying to figure why your results don't display, that could very well be the reason.

So far this piece of code works great:

BeginJS
var req = new XMLHttpRequest();
req.open('GET', 'http://mydomain/Downloads/Test.txt', false);
req.send(null);
if (req.readyState==4);
$rootScope.TestVar = req.responseText;
$rootScope.TestArray = $rootScope.TestVar.split("\r\n");
EndJS

Thank you PaulTomo and Gaev.

Now on to parsing the array.
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1505
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: Reading and writing local files

Postby Wrangler » Sun Jul 05, 2015 11:41 am

Stuck already. Using this code after code above:

StrParse "[TestVar]" "," [chunk]
AlertBox "" "[chunk3]" ""

This displays 'undefined', so I'm thinkin [chunk3] isn't correct, as you would use in neobook. What is the correct way to display a chunk of an array?
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1505
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: Reading and writing local files

Postby PaulTomo » Sun Jul 05, 2015 12:22 pm

If you want the 3rd result then you use [chunk(2)]

This should explain.

Re: Variables and Arrays

Postby Neosoft Support » Mon Jan 26, 2015 7:38 pm

There is no way for you to know this, but you can reference array elements like this:

SetVar [Letter] "[Alphabet(5)]"

This will place the sixth* element of Alphabet into the variable [Letter]. You can also do this:

SetVar [Letter] "[Alphabet([Pos])]"

This exception to the no nested variables rule works with arrays and in code only.

On-screen, you can display an array element like this:

[Alphabet|element:5]

or with the [Pos] variable:

[Alphabet|element:(Pos)]

*This is the sixth element since the first element is zero "0"

NeoSoft Team

Last edited by PaulTomo on Sun Jul 05, 2015 12:33 pm, edited 1 time in total.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK

Re: Reading and writing local files

Postby Wrangler » Sun Jul 05, 2015 12:29 pm

Thank you, Paul! I just needed the parenthesis.
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1505
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: Reading and writing local files

Postby Wrangler » Sun Jul 05, 2015 1:59 pm

Hmmmm. I notice that when making a change in the file on my server that is read, it isn't reflected when I rerun the app. I just removed the header line in the csv. Is there a cache that needs to be cleared between sessions?

UPDATE: I guess it's a neoappbuilder thing. After closing neoappbuilder down and restarting, it displayed the updated file correctly.
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1505
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA


Return to General NeoAppBuilder Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron