最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cordova's FileTransfer Writing Error (Code 1) - Stack Overflow

programmeradmin0浏览0评论

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

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;
}
Share Improve this question edited Jan 25, 2015 at 11:01 Yako asked Jan 24, 2015 at 18:51 YakoYako 3,4849 gold badges46 silver badges74 bronze badges 4
  • 1 Have you tried to print the localPath when you use it? It should be 'cdvfile://localhost/persistent/path/to/downloads/' and that path should be writable. You can check the writable paths from File plugin. – Roope Hakulinen Commented Jan 24, 2015 at 20:47
  • I think this is a good clue; I get this : //.myApp/contentImages/image.jpg. So I digged back to rootFS and I edited the question. – Yako Commented Jan 25, 2015 at 10:54
  • It seems that upgrading Cordova implied to replace .fullPath by .toURL(). I now get file:///storage/emulated/0//.myApp/contentImages/image.jpg. – Yako Commented Jan 25, 2015 at 12:37
  • @Yako: your path looks androidish – r-hold Commented Mar 22, 2018 at 16:06
Add a comment  | 

3 Answers 3

Reset to default 12

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().

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.

Code 1 can be the access issue. In my case there was a problem with file permissions. If you use sdk version less then 29 (android 10) you have to force user to give read/write permission

add cordova-plugin-android-permissions in config.xml

const filesPermissions = (permissions) => [
 permissions.READ_EXTERNAL_STORAGE,
 permissions.WRITE_EXTERNAL_STORAGE,
];

const checkFilesPermissions = async () => {
  const { permissions } = window.cordova.plugins;
  return Promise.all(filesPermissions(permissions).map(permission => {
    return new Promise((resolve, reject) => permissions.checkPermission(permission, resolve, reject));
  })).then(statuses => statuses.every(status => status.hasPermission));
};

const requestFilesPermissions = async () => {
    const { permissions } = window.cordova.plugins;
    return new Promise((resolve, reject) => {
        permissions.requestPermissions(filesPermissions(permissions), resolve, reject);
  }).then(status => status.hasPermission);
};

// onClickHandler:

if (deviceService.isAndroidPlatform()) {
  try {
    let hasFilesPermissions = await checkFilesPermissions();
    if (!hasFilesPermissions) {
      hasFilesPermissions = await requestFilesPermissions();
    }
    if (!hasFilesPermissions) {
      throw new Error('There is no permissions to download files. Please enable it in system preferences');
    }
  } catch (error) {
    alert(error.message)
  }
}

// and then just download your file using cordova-plugin-file-transfer as usual

Note: you have to enable READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in your AndroidManifest.xml

发布评论

评论列表(0)

  1. 暂无评论