3

THE SITUATION:

In my Ionic 2 app there is a Documents section.

I need to let users download files. That means to actually make them available in the storage of the phone so that the user can open them or do whatever he wants.

It seems there are no errors in the code - yet it doesn't save the document in the public folder.

I have tested it on Android device and emulator. In both cases I cannot find the downloaded file.

THE CODE:

downloadFile()
{
    const fileTransfer = new Transfer();
    let url = 'http://www.example.com/example.txt';

    fileTransfer.download(url, cordova.file.externalDataDirectory + 'example.txt').then((entry) => 
    {
        console.log('file object');
        console.log(entry);
    }, (error) => {
        // handle error
    });
}

THE CONSOLE:

This is the log in the emulator console:

enter image description here

THE DOCUMENTATION:

As you can see I am using cordova.file.externalDataDirectory that it is supposed to save the file in the public folder.

As you can see from the documentation:

cordova.file.externalDataDirectory - Where to put app-specific data files on external storage. (Android)

THE QUESTION:

I don't know.. I am probably missing something.. Do you know how can I download files from Ionic 2 app into the public storage?

halfer
  • 19,824
  • 17
  • 99
  • 186
FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178
  • Connect the device to your laptop and use logcat to see what is going on – dannrob May 02 '17 at 20:16
  • check my answer to similar question http://stackoverflow.com/a/43737153/3521116 also it wont work on laptop/desktop browser. – warl0ck May 10 '17 at 18:46
  • Please note that the community has discussed the use of "home-made tags" in question titles, and has resolved that we'd rather titles should be written in natural, flowing English. [Please read this](https://meta.stackoverflow.com/questions/253028/why-is-removing-tags-from-the-title-suggested-so-often). – halfer Feb 27 '18 at 12:48

1 Answers1

3

Use cordova.file.externalRootDirectory and make sure that you have root in the value of AndroidExtraFilesystems in the config.xml

<preference name="AndroidExtraFilesystems" value="root" />

You can add Download/ for example.

...
    var pathFile= cordova.file.externalDataDirectory + 'Download/';
    fileTransfer.download(url, pathFile + 'example.txt')
                .then((entry) => {
...

You can use 'file:///storage/emulated/0/' if cordova.file.externalRootDirectory is null

William Ardila
  • 1,049
  • 13
  • 22