如何使用文件插件在Apache Cordova中本地保存文件 [英] How to save file locally in Apache Cordova using file plugin

查看:185
本文介绍了如何使用文件插件在Apache Cordova中本地保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache cordova文件插件在本地保存文件,并且出现安全错误



我的代码在下面

  window.resolveLocalFileSystemURL(cordova.file.dataDirectory,function(dir){
console.log( got main dir,dir.name);
dir.getFile( catalog.json,{create:true},function(file){
console.log( got the file,file);
logOb = file;
});
},function(error){
console.log(error.message);
console.log(error.name);
}) ;

控制台错误:

 已确定某些文件对于Web应用程序内的访问是不安全的,或者正在对文件资源进行过多调用。 
SecurityError

我需要添加允许意图标签来启用文件处理吗?



我想我显然丢失了一些东西,但是我尝试在Internet上搜索但没有具体结果。

解决方案

Cordova移动Web视图在默认情况下启用了本地文件访问(但始终由应用程序配置决定)。常规浏览器启用了许多安全功能:其中之一是阻止其他文件(例如js代码)对本地文件的访问。



缺少常见的错误源-允许从文件访问文件 Chrome启动参数。请参阅如何使用以下命令启动html在 ----允许从文件访问文件浏览器模式?有关此的更多信息。



请务必检查Cordova文件插件浏览器古怪也是如此。



您的代码的另一个问题是使用 cordova.file.dataDirectory 快捷方式,根据文档,在浏览器中不受支持:


cordova.file。 dataDirectory-使用内部内存(iOS,Android,BlackBerry 10,Windows)在应用程序沙箱中的持久性和私有数据存储


如果查看 cordova.file。* Directory 列表,您会发现浏览器平台不支持任何cordova快捷方式。 / p>

根据您使用的浏览器,类似 cdvfile:// localhost的路径/ persistent / file file:/// persistent / path / to / entry 应该在浏览器中使用。您可以在浏览器怪癖部分中找到有关此信息的更多信息。文档。


I am trying to save a file locally using Apache cordova file plugin and I am getting a Security Error

My code is below

  window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
    console.log("got main dir", dir.name);
    dir.getFile("catalog.json", {create:true}, function(file) {
            console.log("got the file", file);
            logOb = file;
        });
  }, function(error) {
    console.log(error.message);
    console.log(error.name);
  });

Error on the console:

It was determined that certain files are unsafe for access within a Web application, or that too many calls are being made on file resources.
SecurityError

Do I need to add in allow-intent tags to enable working with files?

I think I am obviously missing something but I have tried searching on Internet without concrete results.

解决方案

Cordova mobile webviews have, by default, local file access enabled (but always ruled by the application configuration). Regular browsers have many security features enabled: one of this is the block of local file access from other files (e.g. js code).

A common source of error is the missing --allow-file-access-from-files launch parameter for Chrome. See How to launch html using Chrome at "--allow-file-access-from-files" mode? for more information about this.

Be sure to check the Cordova file plugin browser quirks also.

Another problem with your code is the use of cordova.file.dataDirectory shortcut that, according to the documentation, isn't not supported in the browser:

cordova.file.dataDirectory - Persistent and private data storage within the application's sandbox using internal memory (iOS, Android, BlackBerry 10, windows)

If you look at the cordova.file.*Directory list, you can see that the browser platform doesn't support any cordova shortcut.

Depending on the browser you're using, a path like cdvfile://localhost/persistent/file or file:///persistent/path/to/entry should be used in the browser. You can find more information about this in the browser quirks section of the documentation.

这篇关于如何使用文件插件在Apache Cordova中本地保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆