Capacitor: file-transfer cordova plugin is not working on Android

Created on 6 Feb 2019  路  10Comments  路  Source: ionic-team/capacitor

Description of the problem:
Downloading a file in the Ionic 4/Capacitor application with file-transfer cordova plugin is not working on Android. It works fine on iOS but immediately fails with 401 error on Android. I tried to debug it in Android Studio and it looks like this plugin methods are not even hit..

Affected platform

  • [x] Android
  • [ ] iOS
  • [ ] electron
  • [ ] web

OS of the development machine

  • [ ] Windows
  • [x] macOS
  • [ ] linux

Other information:
ionic info output

Ionic:

   ionic (Ionic CLI)             : 4.5.0 (/Users/alexryltsov/.nvm/versions/node/v10.14.2/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.0.0
   @angular-devkit/build-angular : 0.12.3
   @angular-devkit/schematics    : 7.2.3
   @angular/cli                  : 7.2.3
   @ionic/angular-toolkit        : 1.2.3

Capacitor:

   capacitor (Capacitor CLI) : 1.0.0-beta.17
   @capacitor/core           : 1.0.0-beta.17

Cordova:

   cordova (Cordova CLI) : 8.1.2 ([email protected])
   Cordova Platforms     : none
   Cordova Plugins       : no whitelisted plugins (4 plugins total)

System:

   Android SDK Tools : 26.1.1 (/Users/alexryltsov/Library/Android/sdk/)
   NodeJS            : v10.14.2 (/Users/alexryltsov/.nvm/versions/node/v10.14.2/bin/node)
   npm               : 6.5.0
   OS                : macOS Mojave
   Xcode             : Xcode 10.1 Build version 10B61

Capacitor version:
see above

node version:
see above

npm version:
see above

CocoaPods version:
1.5.3

Steps to reproduce:
download any file from valid link (having HTTPS!) using file-transfer cordova plugin fails with 401 error

Link to sample project:
N/A

Most helpful comment

You need both, the <access origin="*" /> and whitelist plugin.

All 10 comments

Can you provide a sample app?

Can you provide a sample app?

OK. I will try to create it.
Question - what is the best way to share it?

Best way is a github repository

Here is the repo https://github.com/ryaa/File-transfer-cordova-plugin-not-working-with-capacitor-and-Ionic-4

Please note that debugging works in this project that I created from scratch. Not sure why it does not work in my Ionic 4/Capacitor project.
Based on some debugging I think the problem could be related to whitelisting. The file-transfer cordova plugin requires whitelist cordova plugin (see https://github.com/apache/cordova-plugin-whitelist) to be installed. Besides this, in a cordova based project there are some configuration in config.xml. For example <access origin="*" />

I'm not sure how to enable whitelisting for file-transfer cordova plugin in a capacitor based Ionic project.

Thanks for looking into it.
In res/xml/ there is a config.xml, if you manually add the <access origin="*" /> there, does it work?
If so I'll just add it to the generated config.xml.

It seems to fix the problem :) At least it works in my sample repo.
Thank you very much for very prompt response and help!

Not sure if these details should be added somewhere into capacitor documentation to others.

That file is regenerated, so it鈥檚 not recommended to write there. I鈥檒l change it so it adds that by default. Will reopen this until fixed

For anyone that might be running into this issue still. We found that adding <access origin="*" /> to that config.xml did NOT work. The file downloader plugin still returns false for shouldAllowRequest here: https://github.com/apache/cordova-plugin-file-transfer/blob/b7de6f9d4ef25b76b63a07906dac3a602625664f/src/android/FileTransfer.java#L703

The fix for us was to install the whitelist cordova plugin https://github.com/apache/cordova-plugin-whitelist

You need both, the <access origin="*" /> and whitelist plugin.

Yeah ... took us a while to figure that out....It was not obvious right away.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stripathix picture stripathix  路  3Comments

peterpeterparker picture peterpeterparker  路  3Comments

nicobytes picture nicobytes  路  3Comments

ebk46 picture ebk46  路  3Comments

danielsogl picture danielsogl  路  3Comments