1

I have a iphone app built in phonegap framwork, I am using Filesystem to access remote file but I have problem finding a solution.

I have used following code to list drectories which is working

 window.requestFileSystem(
            LocalFileSystem.TEMPORARY, 0,
            function onFileSystemSuccess(fileSystem) {

            fileSystem.root.getDirectory('test/', {create: false, exclusive: false},function(dirEntry){


                 var directoryReader = dirEntry.createReader();
                 directoryReader.readEntries(function(entries){
                 for ( i=entries.length-1; i>=0; i--) {

                 (entries[i].name.indexOf(".jpg") != -1) {
                    if(entries[i].isFile == true)
                      k++;
                        }
                }

           }
   },fail);
          },
       fail);

});

but if i replace 'test/' with the remote server url I'am getting error. Please help me out. I want to list files and the download all the files from server to local folder on Iphone.

Arti
  • 2,993
  • 11
  • 68
  • 121
  • The HTML5 FileSystem is intended for local use. You can't use it to download files from a server. You need server-side code for that. –  Nov 18 '13 at 07:06
  • thanks for quick response. but i have used HTML5 filesystem to download a single file form server, and it is working. I want to download multiple files hence need to list all the files on the server. – Arti Nov 18 '13 at 07:13
  • If you know the URL of an individual file you can download it. To get a directory listing you need server-side code. –  Nov 18 '13 at 07:17
  • could you please be more specific as to what server side code will i have to use.. – Arti Nov 18 '13 at 07:25
  • I don't have anything specific in mind. A PHP script to return a directory listing and possibly offer downloads will do. Or you could use some other server-side technology. Whatever suits your application –  Nov 18 '13 at 07:29
  • I am only using HTML5 and Javascript in my app as it is built using phonegap. Anyways, thanks. :-) – Arti Nov 18 '13 at 07:34
  • @user1864610 is correct that the [FileSystem](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem) API is for local files only. However, if the web server provides a directory listing, you can obtain it programmatically just as you can manually by requesting the URL of the directory (e.g., `http://test/`). This would require that no `index.html` file is present under `test` and that directory listings aren't disallowed by the web server (see https://stackoverflow.com/q/35404637/5025060). Of course your code then must parse the dir listing, a solution I'm currently after. – CODE-REaD Jan 23 '18 at 13:32

1 Answers1

0

Here is how I solved a similar problem using JavaScript:

  1. Ensure the web server allows directory access to the url you are querying (there is no index.html or index.html file present in the directory and directory listings aren't disallowed; see Read remote directory with client-side javascript?).
  2. Use AJAX to obtain the "raw" listing, then Create a dummy DOM element and add the string to it, e.g.:

    var request = new XMLHttpRequest();
    var el = document.createElement('html');
    request.open("GET", url);
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            el.innerHTML = request.responseText;
            var myLinks = el.getElementsByTagName('a');
            var linkArray = [];
            // Extract file 'a' elements only (skip "Parent Directory," etc.):
            for (var i = 5; i < myLinks.length; i++) {
                linkArray.push(myLinks[i].href.replace(/.*\//g, ""));
            }
            console.log('files: ' + linkArray);
        }
    };
    request.send(null);
    

If all went well, the console.log() statement should print a series of files contained in the directory specified in the url given above.

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60