gau*_*ang 5 typescript ionic2 ionic-native ionic3 angular
我正在寻找文件操作,例如创建,读取和写入文本或登录文件,我进行了很多研究,但是我没有找到合适的例子来做。该链接给出了示例,但当我在打字稿文件中使用该示例时,出现类似[ts]的错误,例如类型'Window'上不存在属性'requestFileSystem'。
像这样的例子
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);
function writeFile(fileEntry, dataObj) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function() {
console.log("Successful file write...");
readFile(fileEntry);
};
fileWriter.onerror = function (e) {
console.log("Failed file write: " + e.toString());
};
// If data object is not passed in,
// create a new Blob instead.
if (!dataObj) {
dataObj = new Blob(['some file data'], { type: 'text/plain' });
}
fileWriter.write(dataObj);
});
}
Run Code Online (Sandbox Code Playgroud)
是否有缺少的东西?还是我做错了方向?请帮助我解决此问题。
您可以轻松使用Ionic 原生文件插件。
ionic cordova plugin add cordova-plugin-file
npm install --save @ionic-native/file
Run Code Online (Sandbox Code Playgroud)