- This topic has 11 replies, 4 voices, and was last updated 11 years ago by mrsam.
-
AuthorPosts
-
Max87MemberHello everybody,
is it possible to open a txt file and insert its data to local database?
I have php code, that works(on server). However, I create offline native app, so using php code is not possible(I think).
Can I use javascript?
My php code is attached.
Thanks for any help.Attachments:
You must be logged in to view attached files.
Max87MemberThanks Octavio, but I’d like to know, if it’s possible to read text file locally(without connection to internet by app, id I include it in website directory).
Which jquery code does that? I can work with Local Storage, I need to save the data from txt file to Local database.
Thanks
SonamGyatoMembernah…not possible.
SonamGyatoMemberIf you can get a webapp to read/write a text file.
i’d be interested to know how also.
support-michaelKeymasterA few thoughts:
1) It sounds like you are pushing for local filesystem access. Yes, no?
– With a native app you can do this but you will have to dick around with file systems, dirs, files, file readers, …2) My suggestion is to use the http infrastructure to access the data file’s contents. It will work for either a native app or webapp (tons easier debugging the webapp version)
3) Ask yourself if you really need a sql db or will a simple persisted dictionary (key>value) do the trick? I avoid messing with sql unless there is a compelling advantage for its use over local storage.
support-michaelKeymasterI gen’d a little mobione app that reads a text file and then saves it to localstorage. I tested this example from Test Center, webapp and native iOS app.
See attachment load-data-running.pngHow it works
I know that MobiOne 1.5 improved the form processing and will perform an asynchronous GET or POST form action to a URL that I specify. So I design my UI and generate code using the Run in Test Center action. Next I manually created a data file named data.txt and copied it into my _www folder.
Note: if my app data is structured and I’m using javascript to process it then I typically prefer to structure the data in JSON format. Then I let javascript handle parsing the files content into a structured javascript object which is easy to work withContents of data.txt follows data1 123 abc data2 456 def data3 789 ghi
See attachment load-data-files.png
Lastly I configured the Form properties of my UI and add the “Data File” button that is configured with the Submit Form action. And added logic to the form’s postSubmit handler function that will access the string contents of the data.txt file, update the UI with the text and save it to localstorage.
See attachment load-data-form.png/** * Perform custom actions upon return from form submission. * * @param {boolean} isSuccess true if all OK; otherwise false * @param {Object} data results of form processing; error message if isSuccess == false * @return {boolean} true if OK; otherwise false */ phoneui.postSubmitForm_m1_load_data = function(isSuccess, data) { // add custom postubmission processing code here, // e.g., parse and process results & update UI controls with data as needed // return false to terminate form processing var result = true; if (isSuccess) { // process data displayData(data); saveToLocalStorage(data); result = true; } else { // submit failed displayData("Unable to load data from text file"); result = false; } return result; }
Example Code
See attachment load-data.zipAttachments:
You must be logged in to view attached files.
Max87MemberHi Wayne,
thx for example, I will test it.
To answer your questions, I’m creating an app which uses local storage database and has several tables and a huge amount of entries. I have to use local storage, because app has to work offline(absolutely no internet connection).
I wanted to use txt file to read data from it and add it to database. I have approx. 400 entries for one table and add it manually is not a good idea 🙂
Max87MemberHi Wayne,
your example is working, however it’s not working in offline mode.Is it possible to read files using combo box selection? I have two combo boxes, the value selected will represent name of txt files, I read it to array/structure and insert it to database(tables will be also named according to selected values from combo boxes)
Thx for help
support-michaelKeymasterAs a reminder this example and offline discussion is for a webapp, not a native app.
The underlying mechanisms of my example should work when offline with some configuration tweaking. But I ran a few tests and for some reason no joy. We are discussing with the dev team to understand what might be goof’n up. In a nutshell our idea is to use the HTML5 appcache to load and cache a data file (your text file). Then to read that file when offline (standalone mode) using conventional ajax calls similar to when online; only the appcache should intercept and return the data file. In my testing of this approach the data file retrieval when in offline mode is bonked. We will figure this out.
And yes, you can use combobox widget to select a file to read. We just need to get the datafile loaded when offline.
Max87MemberThanks Wayne for reply, I hope you and dev team will figure this out soon.
What’s the difference in native and webapp for reading file? I understand, if I code a native app using Xcode, reading file requires different approach than in webapp.
But if you can solve the offline reading problem, then I can use it as I need to.
Big thanks for your support.
Max87MemberHello,
is there any news about how to read a text file when offline?
Thx.
mrsamMemberThank you very much guys… the sample is very useful. Now I can manipulate the apps the way I want it to be based on your sample.
Thank you very much.
-
AuthorPosts