我无法在我的离子应用程序中创建和写入文本文件

I can't create and write a text file in my ionic app

我正在尝试创建一个文件,然后写入其中,它说它创建了,但是当我转到该文件夹​​时,那里什么也没有。

有人能告诉我我做错了什么吗?

这是我的代码:

this.file.createFile(this.file.dataDirectory, array.ERROR + '_error', true).then(() => {
                    console.log("Created the file!" + this.file.dataDirectory.toString());
                  }).catch(err => console.log(err));


                  let blob_toSend = new Blob([this.inputxml], { type: 'text/plain' });
                  this.file.writeFile(this.file.dataDirectory, array.ERROR + '_error', blob_toSend, {replace: true, append: false}).then(() => {
                    console.log("Wrote the file!");
                  }).catch(err => console.log(err));

我试着给它起一个像“_error.txt”这样的名字,但它也不起作用。该文件未显示。 提前致谢

在你的ts文件中:

export class HomePage {
  fileValue:string;
  constructor(private plt:Platform,private file: File) {}

  checkValueExist(){
    return (this.fileValue);
  }

  createFile(){
    if(this.checkValueExist()){
      this.file.createFile(this.file.dataDirectory,this.fileValue,true).then(xfile=>{
        let filePath = xfile.nativeURL.substr(0, xfile.nativeURL.lastIndexOf('/') + 1);
        if (this.plt.is('android')) {
          this.copyToAppAndroidDir(filePath, xfile.name, this.createFileName(xfile.name));
        }else{
          this.copyToAppIosDir(filePath, xfile.name, this.createFileName(xfile.name));
        }
      }).catch(err=>{
        alert('File Not Created cause of : '+ JSON.stringify(err));
      })
    }else{
      alert('empty text');
    }
  }

  createFileName(name) {
    var newFileName = name + ".txt";
    return newFileName;
  }

  copyToAppAndroidDir(namePath, currentName, newFileName) {
    this.file.copyFile(namePath, currentName, this.file.externalDataDirectory, newFileName).then(success => {
      alert('done');
    }, error => {
      console.log('err :', error);
    });
  }
  copyToAppIosDir(namePath, currentName, newFileName) {
    this.file.copyFile(namePath, currentName, this.file.dataDirectory, newFileName).then(success => {
      alert('done');
    }, error => {
      console.log('err :', error);
    });
//here in the this.file.dataDirectory for ios it may work or could be tempDirectory since in reality i have android device and don't know where ios store its files so you can change when testing on ios and know the directory.
  }
}

并且在 html 中:

<ion-input type="text" [(ngModel)]="fileValue"></ion-input>

  <ion-button color="primary" shape="round" expand="block" (click)="createFile()">Create File</ion-button>