Cordova 的 FileTransfer 写入错误(代码 1)

Cordova's FileTransfer Writing Error (Code 1)

我正在为 Android 使用 Cordova 4.2.0。

我在 FileTransfer plugin 正常工作时遇到了一些麻烦。估计是写错了

exception:".myApp\/contentImages\/20150110220101.jpg: open failed: ENOENT (No such file or directory)"

文件名之前已经过测试,目前还不存在:

rootFS.getFile('.myApp/contentImages/'+file,{create:false},
    function(){
        console.log(file+' already exists');
    },
    function(error){
        console.log(file+" does not exist locally");
        console.log("Error #"+error.code);
        download(file);
    }
);

这里是下载函数:

function download (filename){
    var localPath = rootFS.fullPath+'/.myApp/contentImages/'+filename;
    var fileTransfer = new FileTransfer();
    fileTransfer.download(
        encodeURI('http://distantApp/contentImages/'+filename), // This file exists
        localPath,
        function(entry) {
            console.log("download complete: " + entry.fullPath);
        },
        function (error) {
            console.log('download error: ' + error.code + ": "+error.exception+" ; source " + error.source+" ; target " + error.target);
        }
    );
}

可能是什么问题?

编辑 rootFS

代码
function onDeviceReady(){
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function(){
        console.log("error requesting LocalFileSystem");
    });
}
function gotFS(fileSystem) {
    console.log("got filesystem: "+fileSystem.name); // displays "persistent"
    console.log(fileSystem.root.fullPath); // displays "/"
    window.rootFS = fileSystem.root;
}

我认为您的问题不在于 FileTransfer 插件,而是您尝试检查文件是否存在的方式。

看这里:http://www.html5rocks.com/en/tutorials/file/filesystem/ 你会看到访问其直接父级不存在的文件会引发异常:

Inside the callback, we can call fs.root.getFile() with the name of the file to create. You can pass an absolute or relative path, but it must be valid. For instance, it is an error to attempt to create a file whose immediate parent does not exist.

我想知道问题是不是您的文件的父文件不存在。在这种情况下,文件夹 .myapp 和 contentImages.

问题是由于 Cordova 从以前的版本升级引起的。

未正确识别本地文件的路径:.fullPath现已过时,应替换为 .toURL()