title: ionic 5 capacitor file download from server using PHP
body: <p>I stuck on file/attachment download in ionic capacitor.</p>
  <p>For this I used below methods but not working, Can anyone can suggest me on this?</p>
  <p>First Method:-</p>
  <pre><code>download(fileURL: any, filename: any) {
  
  this.platform.ready().then(() =&gt; {
  
        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) =&gt; {
          console.log('download complete: ' + entry.toURL());
        }, (error) =&gt; {
          console.log('download EROR: ' + JSON.stringify(error));
        });
        });
    }
  </code></pre>
  <p>**</p>
  <blockquote>
  <p>Result:- Err, error code 3 status 401</p>
  </blockquote>
  <p>**</p>
  <p>Second method:-</p>
  <pre><code>download(fileURL: any, filename: any) {
  
  this.platform.ready().then(() =&gt; {
  
        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: &quot;arraybuffer&quot; }).subscribe(
          (imageBlob: any) =&gt; {
              console.log('Blob' + imageBlob);
              return this.file.writeFile(path, filename, imageBlob, { replace: true, append: false }
              ).then(file =&gt; { console.log(JSON.stringify(file)); })
                .catch(err =&gt; {
                  alert(JSON.stringify(&quot;Error 1: &quot; + err));
                });
            
          }, error =&gt; {
            alert(JSON.stringify(&quot;Error 2: &quot; + error));
            console.log(error);
          }
        );
      });
     }
  </code></pre>
  <p>Result:- No err, but file do not save in device.</p>
  <p>**</p>
  <blockquote>
  <p>Simulator result:- I/Capacitor/Console: File: http://localhost/main.js</p>
  <ul>
  <li>Line 181 - Msg: {&quot;isFile&quot;:true,&quot;isDirectory&quot;:false,&quot;name&quot;:&quot;favicon__1_16244547901624879637.png&quot;,&quot;fullPath&quot;:&quot;/favicon__1_16244547901624879637.png&quot;,&quot;filesystem&quot;:&quot;&lt;FileSystem:
  files&gt;&quot;,&quot;nativeURL&quot;:&quot;file:///data/user/0/com.ionic.app/files/favicon__1_16244547901624879637.png&quot;}</li>
  </ul>
  </blockquote>
  <p>**</p>
  <p>download.php file:-</p>
  <pre><code>//used for download attachment
  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Headers: *');
  header(&quot;Access-Control-Allow-Methods: GET&quot;);
  
  $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(&quot;Pragma: public&quot;);
      header(&quot;Expires: 0&quot;);
      header(&quot;Content-Description: File Transfer&quot;);
      header(&quot;Cache-Control: must-revalidate, post-check=0, pre-check=0&quot;);
      header('Content-Type: application/octet-stream');
      header(&quot;Content-Disposition: attachment; filename=&quot; . $varFile);
      header(&quot;Content-Transfer-Encoding: binary&quot;);
      header(&quot;Content-Length: &quot; . filesize($varFilePath));
  
      @readfile($varFilePath);
      exit();
  }
  </code></pre>