Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download file from url in Ionic 5 without using FileTransfer

I'm currently developing Ionic app now and stuck at file download part. I saw many posts that FileTransfer cordova library is now deprecated in favor of XHR request.

Even though I saw many posts that the library is deprecated, I can't find any sample code (for downloading file from URL).

Could anyone please suggest me good way to download file from url without using FileTransfer plugin?

like image 888
Svetoslav Atanasov Avatar asked May 19 '20 13:05

Svetoslav Atanasov


People also ask

How to transfer files from one server to another in ionic?

First, install a File Transfer plugin to our ionic app using the following steps. Now install the file plugin to our ionic app using the following steps. Now file transfer plugin is already installed so we need to add File Transfer plugin to our app module file. trustAllHosts, (optional, defaults to false.

Is filetransfer Cordova deprecated in ionic?

I'm currently developing Ionic app now and stuck at file download part. I saw many posts that FileTransfer cordova library is now deprecated in favor of XHR request. Even though I saw many posts that the library is deprecated, I can't find any sample code (for downloading file from URL).

How to download and see files via ionic-native file in Xcode?

import { HttpClient } from '@angular/common/http'; import { File } from '@ionic-native/file/ngx'; To be able to download and see files via ionic-native File I had to add additional settings inside Xcode: Step 2: Use the download function to convert it into an appropriate Blob.

How to create an ionic 3 app in NodeJS?

Installation is finished, so let’s start creating an Ionic 3 app. Open your node.js terminal and open a specific location to create the Ionic 3 app. This command will take a few minutes because it installs all dependencies and modules for a project. Now, change the location to the myApp.


2 Answers

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: 'app-home',
   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);
        })
     }
  }
}
like image 119
Arup Bhattacharya Avatar answered Oct 08 '22 08:10

Arup Bhattacharya


You can just open the url with window. But this will open a browser and download the file there. It will do the trick although it is not pretty:

your.page.ts:

function download(url){
  window.open(url, "_blank");
}

your.html:

<a href="javascript:void(0)" (click)="download(yourUrl)">Your file name</>
like image 1
Pouria Moosavi Avatar answered Oct 08 '22 09:10

Pouria Moosavi