Phonegap:在特定文件夹中创建文件

时间:2013-01-24 12:07:09

标签: file cordova directory

我的目标是创建一个像“/ sdcard / files / excel /”或“/ sdcard / files / pdf /”这样的文件夹。 sdcard之后的部分来自url(“/ files / excel”)。首先,我想检查“/ files / excel”是否存在,然后创建一个文件(如果它也不存在)。该名称来自名为“localFileName”的网址。

在这种情况下,folder =“files / excel”和localFileName =“Sheet1.html”。

在fs.root.getDirectory行之后,我收到了名为FileError.PATH_EXISTS_ERR的错误12 但是SD卡中没有文件夹或文件。

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
  var folder = file_path.substring(0,file_path.lastIndexOf('/'));
  console.log(folder);
  fs.root.getDirectory(folder,{create: true, exclusive: false},function (datadir) {
    console.log(folder);
    datadir.getFile(localFileName, {create: true, exclusive: false},function(fileEntry) {
      var ft = new FileTransfer();
      yol = "/sdcard/"+folder+localFileName;
      ft.download( remoteFile,yol,function(entry) {
        console.log(entry.fullPath);
      }, fail);
    }, fail);
  }, fail);
}, fail);

1 个答案:

答案 0 :(得分:1)

cordova-phoengap有一个简单的文件管理器:

https://github.com/torrmal/cordova-simplefilemanagement

您可以递归创建目录:

//CREATE A DIRECTORY RECURSIVELY

new DirManager().create_r('folder_a/folder_b',Log('created successfully'));
相关问题