Transfer to native file.dataDirectory() from Assets folder

Hi there, I’m hoping this is posted in the right section, but please feel free to let me know if i should re-open the topic in another thread if it is best off somewhere else.

The Question: Is there a way to transfer a file from:

assets/folder/file.pdf

to:

file.dataDirectory() (the native directory for files to go)

Currently i have the following code, with a file (say file.pdf) located in ‘assets/folder/file.pdf’ which i am hoping to bring the ‘open with’ section in iOS/android up such that the file can be opened in another PDF filler app such as adobe reader. After scouring the web, it seems my best bet is with the ‘cordova-file-opener2’ plugin which specifically allows for opening files in external applications. However one of the slight issues that happens with this plugin is that files must be opened from a native storage location, and not the ‘assets/folder’ folder, hence my need to get the PDF currently residing in ‘assets/folder’ to the native folder.

CODE so far:

this.file.checkFile(this.file.dataDirectory, 'file.pdf').then((result:boolean) => {
  //annoying quirk with plugin means 'result' wont return false, instead i have to use catch to know the file doesn't exist
}).catch((err) => { 
  this.file.moveFile('assets/folder/', 'file.pdf', this.file.dataDirectory, 'file.pdf').then(() => {
    this.fileOpener.open(this.general.dataDirectory + 'file.pdf', 'application/pdf').then(() => {
      alert('success');
    }); 
  });
});

I also have catch blocks put onto the code to alert out any errors, and am currently getting the following:

{"code":5,"message": "ENCODING_ERR"}

from the .moveFile() section of the code, to which i assume is due to not finding file.pdf in question. Now i understand that the documentViewer plugin allows the same functionality, however i am hoping to just show the open in file straight away rather than showing a blank file and hoping the user will click the ‘share’ arrow in the top right to open in another application (Bad UX).

Alternatively if there is a way to get the file.pdf straight into storage without having to transfer it from the assets folder first, i assume this would work too.

Any help to get this issue sorted is much appreciated in advance. Thank you.

Did you ever get anywhere with this? I’ve got the exact same issue. The only thing im trying to do is open a local PDF file. I thought copying it to the external storage would mean I could then open it in the In App Browser which doesnt seem to be able to access the local assets folder. But im getting the same “ENCODING_ERR”. :frowning:

Hi Gemma,

Unfortunately no one replied to this on here or my stack overflow, so im still left in the dark. I resorted to downloading the file using the filetransfer plugin in the end.

The assets folder is completely invisible to the native filesystem. It lives in a virtual filesystem that is baked into your app binary. Any attempts to treat things inside it like native device files are doomed to failure. One could probably do the read side of what OP is asking for using HTTP, though.

You can try this

  this.file.copyFile(this.file.applicationDirectory+ 'www/assets/', fileName, this.file.dataDirectory, fileName)
            .then(data=>{
              console.log(" file copied ")
              resolve(true)
            })
            .catch(err=>{
              reject()
            })
1 Like

Not working, i am getting Not found error

I have success with this:

let path = this.filePath.resolveNativePath(this.file.applicationDirectory + "www/" + assetsFilePath).

then i use the path to file.copyFile(path, currentName, this.file.dataDirectory, newFileName)

hope helps you.

2 Likes

Try this to access ‘www/assets’ app folder for Android device:

let filePath = this.file.applicationDirectory + 'public/assets/';
2 Likes

This has worked for me (December 2020) to open local PDFs on iOS and Android

        let filePath = this.file.applicationDirectory + 'www/assets/';
        let fileName = guide.pdf
        
        let fakeName = Date.now();

        this.file.copyFile(filePath, fileName, this.file.dataDirectory, `${fakeName}.pdf`).then(result => {
            this.fileOpener.open(result.nativeURL, 'application/pdf');
        });

still not able to access ERROR : {“code”:1,“message”:“NOT_FOUND_ERR”}