javascript Cordova 的 FileTransfer 写入错误(代码 1)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28129044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cordova's FileTransfer Writing Error (Code 1)
提问by Yako
I'm using Cordova 4.2.0 for Android.
我在 Android 上使用 Cordova 4.2.0。
I have some troubles to get FileTransfer pluginwork properly. I suppose that there is a writing error
我在使FileTransfer 插件正常工作时遇到了一些麻烦。我想有一个写作错误
exception:".myApp\/contentImages\/20150110220101.jpg: open failed: ENOENT (No such file or directory)"
filenamewas 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?
可能是什么问题呢?
EDITCode for rootFS
编辑代码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;
}
回答by Yako
The problem was caused by an upgrade of Cordova from a previous version.
该问题是由 Cordova 从以前的版本升级引起的。
The path of local files was not properly identified: .fullPath
is now obsolete and should be replaced by .toURL()
.
未正确识别本地文件的路径:.fullPath
现在已过时,应替换为.toURL()
.
回答by nespapu
I think your problem is not with FileTransfer plugin, but the way you are trying to check if the file exists.
我认为您的问题不在于 FileTransfer 插件,而在于您尝试检查文件是否存在的方式。
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:
看这里: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.
在回调中,我们可以使用要创建的文件名调用 fs.root.getFile()。您可以传递绝对或相对路径,但它必须是有效的。例如,尝试创建其直接父级不存在的文件是错误的。
I am wondering if the problem is that the parents of your file don't exist. In this case the folders .myapp and contentImages.
我想知道问题是否是您文件的父项不存在。在这种情况下,文件夹 .myapp 和 contentImages。