How can i list files of a directory ?

How can i list files of a directory ?

i use cordova intel xdk, I want Show it

 

 

thx

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I recommend that you look into the Core Cordova FIle plugin (https://github.com/apache/cordova-plugin-file).

this is part of my code

 <script src="js/app.js"></script>

        <script src="js/init-app.js"></script>

        <script src="xdk/init-dev.js"></script>

        <script type="application/javascript" src="lib/jquery.min.js"></script>
        <script type="application/javascript" src="marginal/marginal-position.min.js"></script>

        <script src="intelxdk.js"></script>

        <script type="application/javascript" src="camera/cordova-cam.js"></script>
        <script src="../plugins/cordova-sqlite-storage/www/SQLitePlugin.js"></script>
        <script src="../plugins/cordova-plugin-file/www/FileSystem.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryReader.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryEntry.js"></script>
        

        <script type="text/javascript">
            document.addEventListener("deviceready", onDeviceReady, false);

            function onDeviceReady() {
                
                  requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);
                
                    
                
             
        function onSuccess(fileSystem) 
        {   
      
            var directoryEntry = fileSystem.root;
            
            //lets create a file named readme.txt. getFile method actually creates a file and returns a pointer(FileEntry) if it doesn't exist otherwise just returns a pointer to it. It returns the file pointer as callback parameter.
            
       
            
            directoryEntry.getFile("dcim/Camera/MYARCHIVE.txt", {create: true, exclusive: false}, function(fileEntry){
             
                fileEntry.createWriter(function(writer){
                    writer.write("This is the text inside readme file");
                }, function(error){
                    console.log("Error occurred while writing to file. Error code is: " + error.code);
                });
            }, function(error){
                console.log("Error occurred while getting a pointer to file. Error code is: " + error.code);
            });
            

            // SO FAR SO GOOD, BUT NOW I WANT LIST  FILE MYARCHIVE.TXT  here down, one questions where give it  the path directory?

// and run my app show me the next error  "filed to list directory contents : error code is 1"
            
     //-----------------------------------------------------------    
           
            var directoryEntry = fileSystem.root;
            
          
            //object to read the contents of the directory
            var directoryReader = directoryEntry.createReader('dcim/Camera');
                        
            alert(directoryEntry.fullPath); // esto esta bien
                
            //now read the contents using the readEntries function.
            directoryReader.readEntries(function(entries){
                var i;
                for (i=0; i<entries.length; i++) 
                {
                     alert(entries[i].name);
                   
                }
            },function(error){
                alert("Failed to list directory contents. Error code is: " + error.code);
            });

 

 

 

thankyou por you helps i'm crazy with this problem

 

Jaime Muñoz Oyarzún
        

Jaime -- where is your cordova.js reference? You should not be doing this:

        <script src="../plugins/cordova-sqlite-storage/www/SQLitePlugin.js"></script>
        <script src="../plugins/cordova-plugin-file/www/FileSystem.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryReader.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryEntry.js"></script>

The cordova.js script takes care of this, along with many other things having to do with adding and initializing the Cordova plugins. Your app will not work by doing what you are doing.

Also, mobile devices do not allow you to read any location on the system, like a PC does. However, you should be able to read the DCIM directory, but there is no guarantee. The doc page for the file plugin contains lots of good information regarding system directory layouts and how to use the file plugin to read and write files on a real device > https://github.com/apache/cordova-plugin-file

I recommend you use the "Blank Cordova Starter App" in the "Samples and Demos" section you get to by selecting the blue "Start a New Project" button at the bottom left of the Projects tab as a starting point for your app.

I have cordova.js  in my project 

 

<!DOCTYPE html>
<html>

    <head>
        <script>
            (function() {
                if ("-ms-user-select" in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/)) 
                {
                    var msViewportStyle = document.createElement("style");
                    msViewportStyle.appendChild(document.createTextNode("@-ms-viewport{width:auto!important}"));
                    document.getElementsByTagName("head")[0].appendChild(msViewportStyle);
                }
            })();
        </script>

        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
        <meta charset="UTF-8">
        <title>Blank App Designer Packaged Web App Project Template</title>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
        <link href="css/fonts.css" rel="stylesheet" type="text/css" charset="utf-8">
        <link href="css/main.css" rel="stylesheet" type="text/css">

         <script src="cordova.js"></script>          <!-- "Phantom" cordova.js required for projects that use plugins -->

        <script src="js/main.js" type="text/javascript"></script>
 
        <style>
            @-ms-viewport { width: 100vw ; min-zoom: 100% ; zoom: 100% ; }       @viewport { width: 100vw ; min-zoom: 100% zoom: 100% ; }
                @-ms-viewport { user-zoom: fixed ; min-zoom: 100% ; }            @viewport { user-zoom: fixed ; min-zoom: 100% ; }
        </style>

        <link rel="stylesheet" href="css/app.css">
        <link rel="stylesheet" type="text/css" href="css/index_main.less.css" class="main-less">

        <script src="js/app.js"></script>

        <script src="js/init-app.js"></script>

        <script src="xdk/init-dev.js"></script>

        <script type="application/javascript" src="lib/jquery.min.js"></script>
        <script type="application/javascript" src="marginal/marginal-position.min.js"></script>

        <script src="intelxdk.js"></script>

        <script type="application/javascript" src="camera/cordova-cam.js"></script>
        <script src="../plugins/cordova-sqlite-storage/www/SQLitePlugin.js"></script>
        <script src="../plugins/cordova-plugin-file/www/FileSystem.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryReader.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryEntry.js"></script>
    
        

        <script type="text/javascript">
            document.addEventListener("deviceready", onDeviceReady, false);

            function onDeviceReady() {
                
                alert("creo 1");
                
               requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);
                
                
                 var db = database();
                
                    function database(){
                        db = window.sqlitePlugin.openDatabase({name: "medidores.db", createFromLocation: 1});
                        db.transaction(function(tx){
 tx.executeSql("CREATE TABLE IF NOT EXISTS lectura(fecha_lectura VARCHAR(10),cliente    VARCHAR(30), numero_medidor    VARCHAR(20), nombre_cliente VARCHAR(30), descripcion_sector    VARCHAR(30), descripcion_comuna    VARCHAR(30), lectura integer);", [], null, null); });
        
                        
                    return db;
            }
                
             
        function onSuccess(fileSystem) 
        {   
      
            var directoryEntry = fileSystem.root;
            
            //lets create a file named readme.txt. getFile method actually creates a file and returns a pointer(FileEntry) if it doesn't exist otherwise just returns a pointer to it. It returns the file pointer as callback parameter.
            
       
            
            directoryEntry.getFile("dcim/Camera/file.txt", {create: true, exclusive: false}, function(fileEntry){
                //lets write something into the file
                fileEntry.createWriter(function(writer){
                    writer.write("This is the text inside readme file");
                }, function(error){
                    console.log("Error occurred while writing to file. Error code is: " + error.code);
                });
            }, function(error){
                console.log("Error occurred while getting a pointer to file. Error code is: " + error.code);
            });
            
            
            
     //-----------------------------------------------------------    
           
            var directoryEntry = fileSystem.root;
            
          
            //object to read the contents of the directory
            var directoryReader = directoryEntry.createReader('dcim/Camera');
            
          
            
            alert(directoryEntry.fullPath); // esto esta bien
                
            //now read the contents using the readEntries function.
            directoryReader.readEntries(function(entries){
                var i;
                for (i=0; i<entries.length; i++) 
                {
                     alert(entries[i].name);
                   
                }
            },function(error){
                alert("Failed to list directory contents. Error code is: " + error.code);
            });
        
       

 

thx    

            

I recommend that you remove the following lines in your code as they are not necessary and can contribute to your issues:

 <script src="intelxdk.js"></script>

        <script type="application/javascript" src="camera/cordova-cam.js"></script>
        <script src="../plugins/cordova-sqlite-storage/www/SQLitePlugin.js"></script>
        <script src="../plugins/cordova-plugin-file/www/FileSystem.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryReader.js"></script>
        <script src="../plugins/cordova-plugin-file/www/DirectoryEntry.js"></script>

 

thx

Leave a Comment

Please sign in to add a comment. Not a member? Join today