【问题标题】:Phonegap: Create a file in a specific folderPhonegap:在特定文件夹中创建文件
【发布时间】:2013-01-24 12:07:09
【问题描述】:

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

在本例中是 folder="files/excel" 和 localFileName="Sheet1.html"。

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

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);

【问题讨论】:

  • 我在@dhaval 在这个link 的答案中找到了我自己的解决方案,所以这是我的代码window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) { window.FS = fileSystem; var printDirPath = function(entry){ console.log("Dir path - " + entry.fullPath);} createDirectory(folder,localFileName,remoteFile, printDirPath); }, fail); 我使用了@dhaval 的创建目录方法

标签: file cordova directory


【解决方案1】:

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

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

您可以递归创建目录:

//CREATE A DIRECTORY RECURSIVELY

new DirManager().create_r('folder_a/folder_b',Log('created successfully'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多