Ionic-native: File write, create, readAsText broken

Created on 20 Aug 2016  路  8Comments  路  Source: ionic-team/ionic-native

File writeFile, createFile replace as boolean value = true does not work. readAsText fails expecting a blob . . . I am trying to write a simple text file (that should be replaced every time) and read its contents back . . . [email protected]

This is the only way I could get it to work:

Write:

File.removeFile(window['cordova'].file.dataDirectory, 'file.json')
    .then(() => File.createFile(window['cordova'].file.dataDirectory, 'file.json', false)
        .then(fileEntry => File.writeExistingFile(window['cordova'].file.dataDirectory, 'file.json', JSON.stringify({ key: 'value' })));

Read:
this.http.get(window['cordova'].file.dataDirectory + 'security.json').map(res => res.json().key);

Most helpful comment

File.readAsText still don't work. It seems the promise will not complete forever.
v2.9.0

All 8 comments

Hopefully this PR will address writeFile, but createFile doesn't seem to have a similar problem, so I'm not sure what's going on there.

@pgg the PR was merged and is now released, please update to v1.3.17 and let us know if you still have any issues.

Now writeFile with boolean true only works if there is already a file. Also getting strange behaviour with other File operations all round, will test and log them asap, but I suggest an all round File operations sanity check/test . . .

@ihadeed What is the correct way to read a text file? File.readAsText does not work . . .

All readAsXXXX still having 'not Blob' error, i.e.:

TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

EDIT: #479 fixes this issue.

Took a quick look in sources of File.writeFile() method under src/plugins/file.ts.

It seems like things are messed here:

  • specified WriteOptions via replaceOrOptionsparameter is never used because internal opts object is constructed only for a case when replaceOrOptions is true.
  • opts has WriteOptions type, but it is passed to File.getFile() which expects Flags type.

Hope it'll help to fix the issue.

@pgg Can you try with the latest version v2.2.2 and report back if you still have any issue? Thanks.

File.readAsText still don't work. It seems the promise will not complete forever.
v2.9.0

Was this page helpful?
0 / 5 - 0 ratings