Cordova FileTransfer doesn't download file

Hello!

I am trying to download file with cordova-plugin-file-transfer on Android device and emulator:

public async downloadFile(task: DownloadTaskModel) {
    let path = null;

    if (this.platform.is('ios')) {
      path = this.file.documentsDirectory;
    } else {
      path = this.file.dataDirectory;
      console.log('downloadFile():', path);
    }

    const fileTransfer = this.transfer.create();
    fileTransfer.download(
      task.download_url,
      path + task.publish_file,
      true, {
        headers: {
          'Connection': 'close'
        }
    })
    .then(entry => {
      console.log('downloadFile(): File downloaded: ', entry);
    })
    .catch(error => {
      console.error('downloadFile(): Dowload failed:', error);
    });
  }

But nothing is happenning. fileTransfer.download(…) got response from server but downloading does not start:
Screenshot%20from%202019-06-28%2017-25-55

Should fullPath be “/BigBuckBunny_320x180.mp4” or something longer? Please, tell me what is wrong in response data?

“cordova-plugin-file”: “6.0.1”,
“cordova-plugin-file-transfer”: “1.7.1”,

Ionic:

Ionic CLI : 5.2.0 (/home/artem/.nvm/versions/node/v10.14.1/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.5.0
@angular-devkit/build-angular : 0.13.9
@angular-devkit/schematics : 7.3.9
@angular/cli : 7.3.9
@ionic/angular-toolkit : 1.5.1

Capacitor:

Capacitor CLI : not installed
@capacitor/core : not installed

Cordova:

Cordova CLI : 9.0.0 (cordova-lib@9.0.1)
Cordova Platforms : android 8.0.0, browser 6.0.0
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 3.1.2, (and 13 other plugins)

Utility:

cordova-res : 0.3.0 (update available: 0.5.1)
native-run : 0.2.2 (update available: 0.2.7)

System:

Android SDK Tools : 26.1.1 (/home/artem/SDK/Android)
NodeJS : v10.14.1 (/home/artem/.nvm/versions/node/v10.14.1/bin/node)
npm : 6.9.2
OS : Linux 4.9

The file transfer plugin is deprecated. Can you not use Angular’s HttpClient to achieve your goal instead?

1 Like

Hello.
You may be having the same problem as me: “File already exists”
It errors with “null” as result

Downloading files from another server may cause annoying CORS error which is mostly beyond our control. The safest way is to bypass the Webview and download the file natively. You may use Native HTTP plugin

Use in Ionic 4 or above will look like:

import { Component } from '@angular/core';
import { HTTP } from '@ionic-native/http/ngx';
import { File } from '@ionic-native/file/ngx';
        
@Component({
   selector: 'selector-name',
   templateUrl: './you-file.html',
   styleUrls: ['./your-file.scss'],
})

export class HomePage {
    constructor(private nativeHTTP: HTTP, private file: File) {}
        
     private downloadFileAndStore() {
        //
        const filePath = this.file.dataDirectory + fileName; 
                         // for iOS use this.file.documentsDirectory
        
        this.nativeHTTP.downloadFile('your-url', {}, {}, filePath).then(response => {
           // prints 200
           console.log('success block...', response);
        }).catch(err => {
            // prints 403
            console.log('error block ... ', err.status);
            // prints Permission denied
            console.log('error block ... ', err.error);
        })
     }
  }
}

I have used this in several of my Ionic 4/5 projects and they are working smoothly.

1 Like