2012-06-18 15 views
14

Sto usando API file phonegap per creare una directory e creare un file nella directory creata. La directory viene creata, ma il file non viene creato nella directory.Come creare una directory e un file in quella directory usando phpegap file api?

Il codice che sto usando è:

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    var dataDir = fileSystem.root.getDirectory("data", {create: true}); 
    var file = dataDir.getFile("lockfile.txt", {create: true, exclusive: true}); 
} 

I dati directory viene creata, ma lockfile.txt non è sempre creato.

risposta

27

È necessario chiamare il codice in maniera asincrona:

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    fileSystem.root.getDirectory("data", {create: true}, gotDir); 
} 

function gotDir(dirEntry) { 
    dirEntry.getFile("lockfile.txt", {create: true, exclusive: true}, gotFile); 
} 

function gotFile(fileEntry) { 
    // Do something with fileEntry here 
} 
+0

grazie simon, funziona benissimo ... – mmathan

+0

Quindi, la persona che ha votato questa risposta vuole spiegare perché non funziona per loro? –

+0

Posso sapere dove verrà memorizzata questa directory? – SSS

0

È questo lavoro?

var file = fileSystem.root.getFile("data" + "lockfile.txt", {create: true, exclusive: true}); 
+0

si creano file con questo nome "datalockfile.txt" – Blu

0

file Download dall'URL al dispositivo utilizzando PhoneGap

Si sta lavorando 3.0 e fino a per iOS e Android

var folderName = 'xyz'; 
var fileName; 

function downloadFile(URL) { 
    //step to request a file system 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail); 

    function fileSystemSuccess(fileSystem) { 
     var download_link = encodeURI(URL); 
     fileName = download_link.substr(download_link.lastIndexOf('/') + 1); //Get filename of URL 
     var directoryEntry = fileSystem.root; // to get root path of directory 
     directoryEntry.getDirectory(folderName, { 
      create: true, 
      exclusive: false 
     }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard 
     var rootdir = fileSystem.root; 
     var fp = fileSystem.root.toNativeURL(); // Returns Fullpath of local directory 

     fp = fp + "/" + folderName + "/" + fileName; // fullpath and name of the file which we want to give 
     // download function call 
     filetransfer(download_link, fp); 
    } 

    function onDirectorySuccess(parent) { 
     // Directory created successfuly 
    } 

    function onDirectoryFail(error) { 
     //Error while creating directory 
     alert("Unable to create new directory: " + error.code); 

    } 

    function fileSystemFail(evt) { 
     //Unable to access file system 
     alert(evt.target.error.code); 
    } 
} 

function filetransfer(download_link, fp) { 
    var fileTransfer = new FileTransfer(); 
    // File download function with URL and local path 
    fileTransfer.download(download_link, fp, 
     function(entry) { 
      alert("download complete: " + entry.fullPath); 
     }, 
     function(error) { 
      //Download abort errors or download failed errors 
      alert("download error source " + error.source); 
     } 
    ); 
} 
+0

Il suo lavoro per me ... –

0
function download(URL, fileName){ 
    var folderName = 'xyz'; 
    var uri = encodeURI(URL); 

     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
      function (fileSystem) { 
       var directoryEntry = fileSystem.root; // to get root path of directory 
       directoryEntry.getDirectory(folderName, { 
        create: true, 
        exclusive: false 
       }, onDirectorySuccess, onDirectoryFail); 
       var filename = fileSystem.root.toURL() + folderName + "/" + uri.substr(uri.lastIndexOf("/") + 1); 

       var fileTransfer = new FileTransfer(); 
       fileTransfer.download(uri, filename, 
        function(entry) { // download success 
         var path = entry.toURL(); //**THIS IS WHAT I NEED** 
         window.plugins.toast.showLongBottom("Download Completed: " + entry.fullPath, function (a) { 
         }, function (b) { 
         }); 
        }, 
        function(error) { 
         console.log("error") 
        } // irrelevant download error 
       );`enter code here` 
      }, 
      function(error) { 
       console.log("error2") 
      } // irrelevant request fileSystem error 
     ); 

     function onDirectorySuccess(parent) { 
      // Directory created successfuly 
      console.log("Directory created successfuly: " + JSON.stringify(parent)); 
      var fp = (parent.nativeURL) + fileName; 
      filetransfer(download_link, fp); 
     } 

     function onDirectoryFail(error) { 
      //Error while creating directory 
      alert("Unable to create new directory: " + error.code); 
     } 
    } 
+0

Questo codice di download funziona per iOS e Android sia con l'ultima versione di Cordova 3.4+ tutti e il framework ionico –

Problemi correlati