gpt4 book ai didi

file - Ionic 2 文件插件使用示例

转载 作者:太空狗 更新时间:2023-10-29 16:54:31 25 4
gpt4 key购买 nike

有没有人有关于如何在 Ionic 2/Angular 2 项目中使用 Cordova native 文件插件的完整示例?

我安装了这个插件,但文档对我来说似乎没有多大意义,因为它是零散的并且缺少完整的示例,包括所有需要的导入。

例如,以下示例不显示 LocalFileSystem 或 window 等对象的来源。

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);

例如,我需要创建一个属性文件。首先我需要检查应用程序沙箱存储区域中是否存在文件,如果不存在我必须创建它。然后我必须打开文件写入数据并保存。我怎么能那样做?

最佳答案

Ionic 2 带有一个 Cordova 文件插件包装器: http://ionicframework.com/docs/v2/native/file/ .

您可以在原始插件的文档中找到必要的文件系统路径(例如 cordova.file.applicationDirectory): https://github.com/apache/cordova-plugin-file#where-to-store-files .请注意,并非所有平台都支持相同的存储路径。

我什至设法用它构建了一个文件浏览器。像这样使用它:

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
}
);

关于file - Ionic 2 文件插件使用示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com