Javascript 科尔多瓦';s文件传输写入错误(代码1)
我正在为Android使用Cordova 4.2.0 我在正常工作方面遇到了一些麻烦。我想有个书写错误Javascript 科尔多瓦';s文件传输写入错误(代码1),javascript,android,cordova,file-transfer,cordova-plugins,Javascript,Android,Cordova,File Transfer,Cordova Plugins,我正在为Android使用Cordova 4.2.0 我在正常工作方面遇到了一些麻烦。我想有个书写错误 exception:".myApp\/contentImages\/20150110220101.jpg: open failed: ENOENT (No such file or directory)" 文件名以前经过测试,但目前不存在: rootFS.getFile('.myApp/contentImages/'+file,{create:false}, function(){
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插件,而是你试图检查文件是否存在的方式 查看此处:您将看到,访问其直接父级不存在的文件会引发异常: 在回调内部,我们可以使用要创建的文件名调用fs.root.getFile()。您可以传递绝对或相对路径,但它必须有效。例如,尝试创建其直接父级不存在的文件是错误的
我想知道问题是否在于你档案的父母不存在。在本例中,folders.myapp和contentImages。问题是由Cordova从以前的版本升级引起的
未正确标识本地文件的路径:
.fullPath
现在已过时,应替换为.toURL()
使用时是否尝试打印本地路径?应该是,cdvfile://localhost/persistent/path/to/downloads/'并且该路径应该是可写的。您可以从中检查可写路径。我认为这是一个很好的线索;我明白了:/.myApp/contentImages/image.jpg
。因此,我回到rootFS
并编辑了这个问题。升级Cordova似乎意味着用.tour()
替换.fullPath
。我现在得到file:///storage/emulated/0//.myApp/contentImages/image.jpg
@Yako:您的路径看起来像是一条机器人路径,但我认为父对象是存在的。我删除了contentImages
目录,并正确地重新创建了它。但是你可能是对的,因为如果目录不存在,我没有将这部分代码粘贴到创建目录的地方,这导致了download()
函数。到目前为止,PhoneGap应该已经真正更新了他们的文档。我花了几个小时想弄明白这个。。。并将其更改为.toURL()修复了它。谢谢