2016-12-08 192 views
1

我有一個使用文件插件的應用程序,在應用程序內一切正常,但我使用的文件沒有出現在文件資源管理器中。Cordova文件沒有創建目錄

我的班級:

function FileCordova() { 

    this.createDir = function(dirName, callback) { 

     App.db.fsSetup(); 
     if (App.db.hasFS()) { 

      window.requestFileSystem(window.PERSISTENT, 0, function (f) { 
       f.root.getDirectory(dirName, {create: true}, function (d) { 
        typeof callback === 'function' && callback(d.name, d.fullPath); 
       }, 
       function(error, err2){ 
       console.log(error); 
       }); 
      }, function(error,err2){ 
       console.log(error); 
      }); 

     } else { 
      typeof callback === 'function' && callback(); 
     } 
    }; 

} 

裏面的應用程序去文件出現,但我不能在根目錄中找到。

回答

2

我剛剛不得不在我的應用程序中執行此操作。我用window.resolveLocalFileSystemURL()方法,而不是window.requestFileSystem()

var exportDirectory = ""; 
var subdir = "MySubdirectory"; 
// What directory should we place this in? 
if (cordova.file.documentsDirectory !== null) { 
    // iOS, OSX 
    exportDirectory = cordova.file.documentsDirectory; 
} else if (cordova.file.sharedDirectory !== null) { 
    // BB10 
    exportDirectory = cordova.file.sharedDirectory; 
} else if (cordova.file.externalRootDirectory !== null) { 
    // Android, BB10 
    exportDirectory = cordova.file.externalRootDirectory; 
} else { 
    // iOS, Android, BlackBerry 10, windows 
    exportDirectory = cordova.file.DataDirectory; 
} 

window.resolveLocalFileSystemURL(exportDirectory, function (directoryEntry) { 
    console.log("Got directoryEntry. Attempting to open/create subdirectory:" + subdir); 
    directoryEntry.getDirectory(subdir, {create: true}, function (subdirEntry) { 
     subdirEntry.getFile(filename, {create: true}, function (fileEntry) { 
      console.log("Got fileEntry for: " + filename); 
      fileEntry.createWriter(function (fileWriter) { 
       console.log("Got fileWriter"); 
       // make your callback to write to the file here... 
      }, exportFail); 
     }, exportFail); 
    }, exportFail); 
}, exportFail); 

注意,但是,Android的文件傳輸實用程序沒有正確地刷新。我不得不斷開/重新連接我的USB電纜,然後出現目錄。這可能實際上是您的文件資源管理器的問題(即在更改代碼之前檢查它)。

+0

謝謝!有用! –

1

我相信問題在於您將文件保存在應用程序數據目錄(私人)中,如果您想放置在公開的某個位置(允許以該方式在​​文件瀏覽器中查看它),您必須查看this docs並選擇一個公共位置,例如在cordova.file.externalRootDirectory

相關問題