# Native file issues

**URL:** <https://forum.ionicframework.com/t/native-file-issues/107815>\
**Category:** Ionic Native\
**Created:** [October 5, 2017, 4:23pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815 "2017-10-05T16:23:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![htbill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/htbill/32/90619_2.png) [@htbill](https://forum.ionicframework.com/u/htbill)\
**Post date:** [October 5, 2017, 4:23pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815/1 "2017-10-05T16:23:07Z")

</div>

I can’t seem to find any info on the error I’m getting when trying to copy a file taken with the camera to data storage. Here is my function (which is pretty much what everyone seems to do):

```auto
  copyFileToLocalDir() {
    
    let d = new Date();
    let n = d.getTime();
    let newFileName = n + ".jpg";
    console.log('new name',newFileName);
    
    this.file.copyFile(this.imagePath, this.imageName, cordova.file.dataDirectory, newFileName)
      .then(success => {
        this.lastImage = newFileName;
      }, error => {
        console.log('error saving');
      });
  }

```

The error I’m getting when running in Android emulator is **ERROR Error: Uncaught (in promise): Invalid action**  
Ionic Info:

```auto
cli packages: (/Users/billb/dev/customer-mkt-app/node_modules)

    @ionic/cli-utils : 1.12.0
    ionic (Ionic CLI) : 3.12.0

global packages:

    cordova (Cordova CLI) : 7.0.1

local packages:

    @ionic/app-scripts : 1.3.7
    Cordova Platforms : android 6.2.3 ios 4.4.0
    Ionic Framework : ionic-angular 3.3.0

System:

    Android SDK Tools : 26.1.1
    ios-deploy : 1.9.0
    ios-sim : 5.0.10
    Node : v7.8.0
    npm : 4.6.1
    OS : macOS Sierra
    Xcode : Xcode 9.0 Build version 9A235

Misc:

    backend : legacy

```

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [October 5, 2017, 5:18pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815/2 "2017-10-05T17:18:17Z")

</div>

What is `this.file`?  
What line is throwing that error?

> [@htbill](#):
>
> @ionic/app-scripts : 1.3.7

This is old and should be upgraded.

---

<div class="post-metadata">

**Author:** ![htbill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/htbill/32/90619_2.png) [@htbill](https://forum.ionicframework.com/u/htbill)\
**Post date:** [October 5, 2017, 5:22pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815/3 "2017-10-05T17:22:10Z")

</div>

**this.file…** is referencing Native file

```auto
import { File } from '@ionic-native/file';
...
  constructor(public navCtrl: NavController, public navParams: NavParams, private StorageProvider: StorageProvider, public viewCtrl: ViewController, private camera: Camera, private file: File, public platform: Platform) {
  }

```

When I tried to use just **File…** it would through an error of copyFile not found on typeof File

the line that throws the error is

```auto
this.file.copyFile(this.imagePath, this.imageName, cordova.file.dataDirectory, newFileName)

```

what version is app-scripts up to?

---

<div class="post-metadata">

**Author:** ![htbill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/htbill/32/90619_2.png) [@htbill](https://forum.ionicframework.com/u/htbill)\
**Post date:** [October 5, 2017, 5:26pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815/4 "2017-10-05T17:26:48Z")

</div>

app-scripts is now at 3.0.0

---

<div class="post-metadata">

**Author:** ![htbill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/htbill/32/90619_2.png) [@htbill](https://forum.ionicframework.com/u/htbill)\
**Post date:** [October 5, 2017, 5:46pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815/5 "2017-10-05T17:46:44Z")

</div>

btw, updating app-scripts didn’t resolve the issue

---

<div class="post-metadata">

**Author:** ![htbill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/htbill/32/90619_2.png) [@htbill](https://forum.ionicframework.com/u/htbill)\
**Post date:** [October 6, 2017, 9:54pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815/6 "2017-10-06T21:54:19Z")

</div>

So I had to dig further and better understand what File plugin is doing. And thanks to the answers on these two questions [here](https://stackoverflow.com/questions/41536303/cannot-read-a-file-using-cordova-file-plugin-in-an-ionic-2-project) and [here](https://stackoverflow.com/questions/27910783/cordova-move-file-using-the-file-url), I finally figured this thing out. Here’s my final code:

```auto
 copyFileToLocalDir() {
    let d = new Date();
    let n = d.getTime();
    let newFileName = n + ".jpg";
    // cordova.file.dataDirectory
    let externalStoragePath: string = cordova.file.dataDirectory;
    
    this.file.resolveLocalFilesystemUrl(this.imagePath + this.imageName)
      .then((entry: any)=>{
        console.log('entry',entry);
      
        this.file.resolveLocalFilesystemUrl(externalStoragePath)
          .then((dirEntry: any)=>{
            
            entry.copyTo(dirEntry, newFileName, this.successCopy, this.failCopy);
          
          }).catch((error)=>{
            console.log(error);
          });

      }).catch((error)=>{
        console.log(error);
      });

  }

```

You have to create 2 objects, one of the current file ( **entry** ) and one for the path to copy the file to ( **dirEntry** ). That was the key to working this out.

---

<div class="post-metadata">

**Author:** ![Sapphire](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Sapphire](https://forum.ionicframework.com/u/Sapphire)\
**Post date:** [September 13, 2019, 8:43am UTC](https://forum.ionicframework.com/t/native-file-issues/107815/7 "2019-09-13T08:43:33Z")

</div>

Hey! I ran into the same problem with ionic 4 and angular.  
I tried to adapt your solution to my code but it crashes at

```auto
const externalStoragePath: string = cordova.file.dataDirectory;

```

with

```auto
Property 'file' does not exist on type 'Cordova'.

```

---

<div class="post-metadata">

**Author:** ![armandiginex](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@armandiginex](https://forum.ionicframework.com/u/armandiginex)\
**Post date:** [November 11, 2019, 8:47am UTC](https://forum.ionicframework.com/t/native-file-issues/107815/8 "2019-11-11T08:47:49Z")

</div>

In case of angular, you need to inject File into your project:

```auto
  constructor(private file: File ) {
     const externalStoragePath: string = cordova.file.dataDirectory;
  }

```

If you necessarily want to use cordova.file the use it like this:

```auto
     const externalStoragePath: string = (cordova as any).file.dataDirectory;

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 8:54pm UTC](https://forum.ionicframework.com/t/native-file-issues/107815/10 "2021-07-19T20:54:22Z")

</div>

Closed on July 19, 2021, 8:54pm UTC


