Cordova's FileTransfer Writing Error (Code 1)

一曲冷凌霜 提交于 2019-12-06 03:41:41

问题


I'm using Cordova 4.2.0 for Android.

I have some troubles to get FileTransfer plugin work properly. I suppose that there is a writing error

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

filename was previously tested and does not exist yet:

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

And here is the download function:

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

What could be the problem?

EDIT Code for 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;
}

回答1:


The problem was caused by an upgrade of Cordova from a previous version.

The path of local files was not properly identified: .fullPathis now obsolete and should be replaced by .toURL().




回答2:


I think your problem is not with FileTransfer plugin, but the way you are trying to check if the file exists.

Looking here: http://www.html5rocks.com/en/tutorials/file/filesystem/ you we'll see that accessing to a file which its immediately parent does not exist raise an exception:

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.

I am wondering if the problem is that the parents of your file don't exist. In this case the folders .myapp and contentImages.



来源:https://stackoverflow.com/questions/28129044/cordovas-filetransfer-writing-error-code-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!