Reputation: 21630
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:
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?
Upvotes: 3
Views: 3531
Reputation: 1049
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
Upvotes: 3