# Ionic-Native File read and write only works in live reload mode

**URL:** <https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230>\
**Category:** Ionic Native\
**Created:** [August 16, 2017, 9:30pm UTC](https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230 "2017-08-16T21:30:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian\_taylor](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@adrian\_taylor](https://forum.ionicframework.com/u/adrian_taylor)\
**Post date:** [August 16, 2017, 9:30pm UTC](https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230/1 "2017-08-16T21:30:53Z")

</div>

Hey Everyone,

I have a strange error that I hope someone can help me with.

I have developed some functionality using the Native File plugin that is used to read and write files to the device storage. This has been working fine during development when running my app with the command:

```auto
ionic cordova run android -l

```

But when I choose to use:

```auto
ionic cordova run android

```

The file read and write functionality doesn’t work?!?

Here is an example of the code that is no longer working:

```auto
saveImageToDevice(projectKey: string, imageData: Blob | string, imageName: string): Promise<any> {
        return new Promise((resolve, reject) => {            
                let options: IWriteOptions = {
                    replace: true
                }
                this.file.writeFile(this.file.externalDataDirectory + projectKey, imageName, imageData, options).then(fileWriteRes => {
                    resolve(fileWriteRes)
                }).catch(err => {
                    console.log("There was an error writing the file")
                    console.error(err)
                })
            })
        })
    }

```

Everything works as expected when I run the app with the live reload option, but when I without the live reload option… the promise just hangs and never resolves or rejects.

What could be causing this??

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 16, 2017, 9:32pm UTC](https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230/2 "2017-08-16T21:32:55Z")

</div>

To eliminate one source of potential problems (and to improve the code quality), get rid of the explicitly instantiated `Promise` entirely, and simply return the one that `writeFile()` gives you.

---

<div class="post-metadata">

**Author:** ![adrian\_taylor](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@adrian\_taylor](https://forum.ionicframework.com/u/adrian_taylor)\
**Post date:** [August 17, 2017, 12:54am UTC](https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230/3 "2017-08-17T00:54:59Z")

</div>

Thanks for the suggestion. I’m returning a promise like that because in my actual code I’m chaining several promises together - I removed the other promise functions in my post for simplicity.

I have half solved my problem…  
Writing files by passing in a Blob would hang and never cause the promise to resolve or reject.  
Changing this to write files by passing in an ArrayBuffer works - any idea why this might be?

Also…

Reading files by using either File.ReadAsArrayBuffer(…) or File.ReadAsDataURL(…) both result in the same promise that just hangs and never resolves…

Any other ideas? I’m stumped

---

<div class="post-metadata">

**Author:** ![adrian\_taylor](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@adrian\_taylor](https://forum.ionicframework.com/u/adrian_taylor)\
**Post date:** [August 17, 2017, 3:04am UTC](https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230/4 "2017-08-17T03:04:01Z")

</div>

Ok I figured it all out… It turns out the way that I was downloading and saving the files was somehow wrong.

I have fixed the issue by using the FileTransfer plugin from ionic-native. In my case I’m using Firebase Storage to store the files… downloading and saving them was trivial. Then I just load them in the application using their native file URI

---

<div class="post-metadata">

**Author:** ![seevoovapp](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@seevoovapp](https://forum.ionicframework.com/u/seevoovapp)\
**Post date:** [November 14, 2017, 12:28pm UTC](https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230/5 "2017-11-14T12:28:32Z")

</div>

Hey.  
we see the exact same behavior with file.readAsDataURL.  
how did you resolved? i am using file transfer to write the file (which works great) but when trying to read the file (without live reload flag enabled) the promise never returns.

appreciate some help.

Thanks Yosi

---

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://forum.ionicframework.com/u/basti)\
**Post date:** [April 25, 2018, 4:31pm UTC](https://forum.ionicframework.com/t/ionic-native-file-read-and-write-only-works-in-live-reload-mode/102230/6 "2018-04-25T16:31:54Z")

</div>

load cordova.js after polyfill.js in index.html made it for me so far.
