Question

I'm trying to get a way to reach and parse all JSON file from a directory, witch inside an Intel Xdk project. All files in my case stored in '/cards' folder of the project. I have tried to reach theme with fs.readdirSync('/cards') method, but that wasn't pointed to the location what I expected, and I haven't got luck with 'intel.xdk.webRoot' path.

With the simulator I've managed to get files, with a trick:

fs.readdirSync(intel.xdk.webRoot.replace('localhost:53862/http-services/emulator-webserver/ripple/userapp/', '')+'cards');

(intel.xdk.webRoot contains absolute path to my '/cards' folder)

and its work like a charm, but it isn't working in any real device what I'd like to build it. I have tried it with iOS7 and Android 4.2 phones.

Please help me to use in a great way the fs.readdirSync() method, or give me some alternate solution.

Thanks and regards,

satire

Was it helpful?

Solution

You can't use nodejs api's on mobile apps. It is a bug that the XDK emulator lets you do it. The bug is fixed in the next release of XDK.

You could write a node program that scans the directory and then writes out a js file with the contents of the directory. You would run the node script on your laptop before packaging the app in the build tab. The output would look like this: files.js:

dir = {files: ['file1.txt', 'file2.txt']}

Then use a script tag to load it:

And your js can read the dir variable. This assumes that the contents does not change while the app is running.

OTHER TIPS

The location of your application's root directory will vary depending on the target platform and can also vary with the emulator and the debug containers (e.g., App Preview versus App Analyzer). Here's what I've done to locate files within the project:

// getWebPath() returns the location of index.html
// getWebRoot() returns URI pointing to index.html

function getWebPath() {
    "use strict" ;
    var path = window.location.pathname ;
    path = path.substring( 0, path.lastIndexOf('/') ) ;
    return 'file://' + path ;
}

function getWebRoot() {
    "use strict" ;
    var path = window.location.href ;
    path = path.substring( 0, path.lastIndexOf('/') ) ;
    return path ;
}

I have not been able to test this exhaustively, but it appears to be working so far. Here's an example where I'm using a Cordova media object and want it to play a file stored locally in the project. Note that I have to "special case" the iOS container:

var x = window.device && window.device.platform ;
console.log("platform = ", x) ;

if(x.match(/(ios)|(iphone)|(ipod)|(ipad)/ig)) {
    var media = new Media("audio/bark.wav", mediaSuccess, mediaError, mediaStatus) ;
}
else {
    var media = new Media(getWebRoot() + "/audio/bark.wav", mediaSuccess, mediaError, mediaStatus) ;
}

console.log("media.src = ", media.src) ;
media.play() ;

Not quite sure if this is what you are looking for...

You will need to use the Cordova build in the Intel XDK, here is information on building with Cordova:

https://software.intel.com/en-us/html5/articles/using-the-cordova-for-android-ios-etc-build-option

And a DirectoryReader example:

function success(entries) {
    var i;
    for (i=0; i<entries.length; i++) {
        console.log(entries[i].name);
    }
}

function fail(error) {
    alert("Failed to list directory contents: " + error.code);
}

// Get a directory reader
var directoryReader = dirEntry.createReader();

// Get a list of all the entries in the directory
directoryReader.readEntries(success,fail);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top