Ionic capacitor file download with PHP

I just started on ionic. all are going good.

But I got stuck on file/attachment download in ionic.

For this I used below two method but no success, Any help will be appreciated.

First Method:-

download(fileURL: any, filename: any) {

this.platform.ready().then(() => {

      const fileTransfer: FileTransferObject = this.transfer.create();
      let url = encodeURI(fileURL); // *PHP FILE PATH download.php*
      let path = null;
      //   //Check plateform 
      if (this.platform.is('ios')) {
        path = this.file.documentsDirectory;
      }
      else {
        path = this.file.externalDataDirectory;
      }
      //   //Process download
      fileTransfer.download(url, path + filename, false).then((entry) => {
        console.log('download complete: ' + entry.toURL());
      }, (error) => {
        console.log('download EROR: ' + JSON.stringify(error));
      });
      });
  }

**

Result:- Err, error code 3 status 401

**

Second method:-

download(fileURL: any, filename: any) {

this.platform.ready().then(() => {

      let url = encodeURI(fileURL);// *PHP FILE PATH download.php*
      let path = null;
      //   //Check plateform 
      if (this.platform.is('ios')) {
        path = this.file.documentsDirectory;
      }
      else {
        path = this.file.externalDataDirectory;
      }

      this.http.get(url, { responseType: "arraybuffer" }).subscribe(
        (imageBlob: any) => {
            console.log('Blob' + imageBlob);
            return this.file.writeFile(path, filename, imageBlob, { replace: true, append: false }
            ).then(file => { console.log(JSON.stringify(file)); })
              .catch(err => {
                alert(JSON.stringify("Error 1: " + err));
              });
          
        }, error => {
          alert(JSON.stringify("Error 2: " + error));
          console.log(error);
        }
      );
    });
   }

Result:- No err, but file do not save in device.

**

Simulator result:- I/Capacitor/Console: File: http://localhost/main.js

  • Line 181 - Msg: {“isFile”:true,“isDirectory”:false,“name”:“favicon__1_16244547901624879637.png”,“fullPath”:“/favicon__1_16244547901624879637.png”,“filesystem”:“<FileSystem:
    files>”,“nativeURL”:“file:///data/user/0/com.ionic.app/files/favicon__1_16244547901624879637.png”}

**

download.php file:-

//used for download attachment
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
header("Access-Control-Allow-Methods: GET");

$varFile = $_GET['file'];
$varDir = ($_GET['dir'] != '') ? $_GET['dir'] : '';

$ImagePath = $arrConfig['sourceRoot'] . $varDir . $varFile;
if (file_exists($ImagePath)) {
    //Download function starts from here
    $varFilePath = SOURCE_ROOT . $varDir . $varFile;
    header("Pragma: public");
    header("Expires: 0");
    header("Content-Description: File Transfer");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename=" . $varFile);
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($varFilePath));

    @readfile($varFilePath);
    exit();
}