Android 如何使用 Apache Cordova 3.4.0 在本地存储文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22336352/
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
How do you store a file locally using Apache Cordova 3.4.0
提问by Nathan
I am having a problem storing a file locally on an iOS (or android) device using apache cordova's "file" plugin. The problem I believe is setting the path properly.
我在使用 apache cordova 的“文件”插件在 iOS(或 android)设备上本地存储文件时遇到问题。我认为问题是正确设置路径。
this is the error message I get from Xcode Could not create path to save downloaded file: The operation couldn\U2019t be completed. (Cocoa error 512.)
这是我从 Xcode 收到的错误消息 无法创建保存下载文件的路径:操作无法完成。(可可错误 512。)
Here is the code where I am attempting to save the file locally:
这是我尝试在本地保存文件的代码:
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
var root;
function onDeviceReady(){
// Note: The file system has been prefixed as of Google Chrome 12:
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onInitFs, errorHandler);
}
function onInitFs(fs) {
var fileURL = "cdvfile://localhost/persistant/file.png";
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://upload.wikimedia.org/wikipedia/commons/6/64/Gnu_meditate_levitate.png");
fileTransfer.download(
uri,
fileURL,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
}
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
alert('Error: ' + msg);
}
</script>
回答by ar34z
Your file path contains a typo (or a grammar error):
您的文件路径包含拼写错误(或语法错误):
var fileURL = "cdvfile://localhost/persistant/file.png";
var fileURL = "cdvfile://localhost/persistant/file.png";
You should write it as persistent.
你应该把它写成Persistent。
Correct code:
正确代码:
var fileURL = "cdvfile://localhost/persistent/file.png";
回答by bottus
Check out these links :
查看这些链接:
http://cordova.apache.org/docs/en/3.4.0/cordova_plugins_pluginapis.md.html#Plugin%20APIshttps://github.com/apache/cordova-plugin-file/blob/dev/doc/index.md
http://cordova.apache.org/docs/en/3.4.0/cordova_plugins_pluginapis.md.html#Plugin%20APIs https://github.com/apache/cordova-plugin-file/blob/dev/doc/index .md
http://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html#File
http://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html#File
First and second links provide you information about the plugin File and how to install it.
第一个和第二个链接为您提供有关插件文件以及如何安装它的信息。
The third one show you how to use the File plugin.
第三个向您展示如何使用 File 插件。
Everytime you need to do something with Cordova, check if a plugin is available to do it :)
每次你需要用 Cordova 做一些事情时,检查是否有插件可以做:)
regards.
问候。
回答by Abram
So far I have only tested this on Android, but I believe it should work as-is, or with little modification on IOS:
到目前为止,我只在 Android 上测试过这个,但我相信它应该可以正常工作,或者在 IOS 上几乎没有修改:
var url = 'example.com/foo'
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
fileSystem.root.getFile('foo_file', {create: true, exclusive: false},
function(file_entry){
var ft = new FileTransfer()
ft.download(url, file_entry.toURL(), function(fe){
fe.file(function(f){
reader = new FileReader()
reader.onloadend = function(ev){
console.log('READ!', ev.target.result)
}
reader.readAsText(f)
})
})
}
)
})
Note that I also needed the contents of the file, so the bit at the end may be omitted if you don't need the contents at the time of downloading.
请注意,我还需要文件的内容,因此如果您在下载时不需要内容,则可以省略末尾的位。
Also note that there is a far simpler method using window.saveAs but it's only available in Android 4.4.
另请注意,使用 window.saveAs 有一个更简单的方法,但它仅在 Android 4.4 中可用。