【问题标题】:How do I write to and read from a file in cordova (ionic) when I have the path of the file to be read?当我有要读取的文件路径时,如何在科尔多瓦(离子)中写入和读取文件?
【发布时间】:2025-12-12 01:15:01
【问题描述】:

有没有以路径为参数的函数?路径格式为file: //storage

【问题讨论】:

标签: javascript android html cordova ionic-framework


【解决方案1】:
    angular.module('starter', ['ionic','ngCordova'])

首先注入 ngCordova 模块,然后在你运行时执行类似的操作

run(function($ionicPlatform,$cordovaFile) {
  $ionicPlatform.ready(function() {
   console.log(cordova.file);
   $cordovaFile.readAsText(cordova.file.externalRootDirectory, "1.txt")
  .then(function (result) {
    console.log(result);
  }, function (error) {
    console.log(error);
  });

如果你想在控制器中使用它

.controller('yourCtrl', function($scope,$cordovaFile) {
ionic.Platform.ready(function() {
console.log(cordova.file);
$cordovaFile.readAsText(cordova.file.externalRootDirectory, "1.txt")
  .then(function (result) {
    console.log(result);
  }, function (error) {
    console.log(error);
  });
});
})

还要确保在索引文件和文件插件中包含 ng-cordova.js 以完成所有这些工作。让我知道是否有任何问题。谢谢

【讨论】: