Ionic 2文件插件使用示例 [英] Ionic 2 File Plugin usage examples
问题描述
有没有人有关于如何在Ionic 2 / Angular 2项目中使用Cordova Native File Plugin的完整示例?
Does anyone have complete examples about how to use the Cordova Native File Plugin in a Ionic 2/Angular 2 project?
我安装了这个插件但是文档没有由于它是碎片化的,缺乏一个完整的例子,包括所有需要的导入,因此似乎对我有意义。
I installed this plugin but the documentation don't seems to make much sense to me due the fact it is fragmented and lacks of a complete example, including all needed imports.
例如,下面的例子没有显示LocalFileSystem或窗口等对象的来源。
For example, the following example don't shows where objects like LocalFileSystem or window came from.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
console.log('file system open: ' + fs.name);
fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) {
console.log("fileEntry is file?" + fileEntry.isFile.toString());
// fileEntry.name == 'someFile.txt'
// fileEntry.fullPath == '/someFile.txt'
writeFile(fileEntry, null);
}, onErrorCreateFile);
}, onErrorLoadFs);
例如,我需要创建一个属性文件。首先,我需要检查应用程序沙箱存储区域中是否存在文件,如果不存在,我必须创建它。然后我必须打开文件写入数据并保存。我怎么能这样做?
For example, I need to crate a property file. First I need to check if a file exists on app sandbox storage area, if don't exists I must create it. Then I must open the file write data and save it . How could I do that?
推荐答案
Ionic 2附带一个Cordova文件插件包装器:
http://ionicframework.com/docs/v2/native/file/ 。
Ionic 2 comes with a Cordova file plugin wrapper: http://ionicframework.com/docs/v2/native/file/.
您可以在原始插件的文档中找到必要的文件系统路径(例如 cordova.file.applicationDirectory
):
https://github.com/apache/cordova-plugin-文件#其中到店,文件。请注意,并非所有平台都支持相同的存储路径。
The necessary file system paths (e.g. cordova.file.applicationDirectory
) you can find here at the documentation of the original plugin:
https://github.com/apache/cordova-plugin-file#where-to-store-files. Note that not all platforms support the same storage paths.
我甚至设法用它构建文件浏览器。像这样使用它:
I even managed to build a file browser with it. Use it like so:
import {Component} from '@angular/core';
import {File} from 'ionic-native';
...
File.listDir(cordova.file.applicationDirectory, 'mySubFolder/mySubSubFolder').then(
(files) => {
// do something
}
).catch(
(err) => {
// do something
}
);
这篇关于Ionic 2文件插件使用示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!