Jump to content
COMFIED

Finding and Opening External File in Drive

Recommended Posts

Hi

 

I am trying to import content from a text ot CSV file located in the phone.

 

Is there a way to locate the file and then read the text content into a memo or text variable?

 

I have seen the JavaScript FileReader object in blogs but I don't know how to use it in SmartMobileStudio.  Any ideas?

 

 

Share this post


Link to post
Share on other sites

Accessing data on the phone might not be possible from a simple web-app (which runs in a browser) due to security reasons. This is especially true if the file is located at a location outside the web-apps directory.

 

If you want to get access to these files you need to use any Cordova-based (or related) technology. There are a few plugins available which can be used to get access to files like photos.

 

In case the file is located inside the web-app range you could also try to use an XMLHttpRequest. This is especially true if conversion of the CSV data to JSON is an option for you.

Share this post


Link to post
Share on other sites

The code you refer to can easily be translated to Smart Mobile Studio. The original API can be found in the unit W3C.File:

uses
  W3C.File, W3C.Console;

// create file reader
var Reader := new JFileReader;

// specify on-load handler as lambda
// (anonymous) functions work as well
Reader.onload := lambda
    // get text
    var Text = Reader.result;

    // output text to the console
    Console.Log(Text);
  end;

// read as text
Reader.readAsText(YourFileName);
As soon as you call Reader.readAsText(YourFileName) with your file of choice, it will call the onload-callback specified in the line before as lambda expression (= simple anonymous function).

 

It uses the console to output the text, but it remains in your hand what to do with the Reader.result.

 

As mentioned before, this might not work on your phone, at least not for older phones/browsers. If converted to a hybrid-app via Cordova you can use the above code if you add the plugin with the name 'cordova-plugin-file'. It will enable the API even on devices that do not support this API natively.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...