IonicNative File writeExistingFile not working?


#1

When I use the method File.writeExistingFile once, it creates a file and writes the content in it. When using it the next time it throws an error: code 12, “PATH_EXISTS_ERR” instead of appending to the file. Am I missing something here? Thanks a lot and have a great holiday!

import { File } from 'ionic-native';
declare var cordova: any;

here is the method:

      storeEntry(entry: string): void {
        File.writeExistingFile(cordova.file.externalApplicationStorageDirectory, "test.txt", entry).then(_ => console.log("written to file")).catch(err => console.log(err));
      }

#2

I found some work-around that I want to share with you:
the method first tries to write to the file with the writeOption {append:true}. If it fails because there is no file yet, it will try again without append:true.
If you’ve got a better solution, I am happy for any input.

File.writeFile(cordova.file.externalApplicationStorageDirectory, "test.txt", entry, {append: true}).then(_ => console.log("written to file")).catch(err => {
  console.log("error in first try: ",err);
  File.writeFile(cordova.file.externalApplicationStorageDirectory, "test.txt", entry).then(_ => console.log("2nd try file created")).catch(err => console.log("2nd try failed ", err));
});

#3

Thanks for sharing.
Had the same problem and used your solution.
I ended up, by first creating the file

const localFolder: string = cordova.file.dataDirectory;    
File.createFile(localFolder,'test.txt', true)
          .then((fileEntry) => {
              // call to File.WriteFile....

Not any better, but maybe somewhat easier to read.


#4

It gives me success on this WriteFile API and when I open the file of test.pdf after this function success it gives me error of Invalid Format.

Any who can help me out in this
Here is my code

 this.file.writeFile(cordova.file.externalDataDirectory,"Test.pdf","Hello World",true).then(succ=>{
      console.log("File write success : ", succ)
    },
    err=>{
      console.log(" write File error : ", err)
    })

#5

in the succes response you have nativeUrl parameter this is the file location the you can use with the nativeUrl and FileOpener plugin to open this file
Example:

this.file.writeFile(cordova.file.externalDataDirectory,"Test.pdf","Hello World",true).then(succ=>{
    console.log("File write success : ", succ)

    this.fileOpener.open(
      succ.nativeURL,
      'application/pdf'//file mimeType
    ).then((success) => {
      console.log('success open file: ', success);
    }, (err) => {
      console.log('error open file', err);
    });
},
err=>{
  console.log(" write File error : ", err)
});

#6

It is giving me success on FIleOpener but when tries to open that file it gives error “Filename.pdf” Invalid Format

I also tried to open that file manually through going into the directory of device but it also gives the same error.

It means WriteFile is not generating a valid format .pdf file.

But it is working fine with .docx and .txt extension.

@avishai_peretz_dev


#7

So you may try to save file as pdf but the file are not pdf file


#8

YES Files are saving as “.pdf” but I when open them it gives me error that Invalid Format


#9

try to transfer the file from your source to your mobile with usb cable and then try to open the file


#10

You have to sanitize it, because angular blocks it in case of XSS-Risks

transform(url: string) {
    return this.sanitizer.bypassSecurityTrustUrl(url)
  }