Skip to content

Issue with file download on iOS #623

@kunalSBasic

Description

@kunalSBasic

Bug Report

Problem

I'm encountering an issue with the cordova-plugin-file plugin in iOS when trying to download and save a PDF file from an API response. The plugin seems to be working fine in Android, but I'm facing difficulties in iOS.

What is expected to happen?

The PDF file should be successfully downloaded and saved to the device's filesystem in iOS, similar to how it's functioning in Android.

What does actually happen?

Code runs successfully and falls under the success block and even present the toast of 'File downloaded successfully.' But file is not downloaded on device.

Information

Command or Code

downloadPdfMobile(base64Data, fileName) {
   const folderPath = this.platform.is('android')
     ? this.file.externalRootDirectory + 'Download/'
     : this.file.documentsDirectory;

   console.log(folderPath, 'FOLDERPATH');

   // Convert base64 to Blob
   const byteCharacters = atob(base64Data);
   const byteNumbers = new Array(byteCharacters.length);
   for (let i = 0; i < byteCharacters.length; i++) {
     byteNumbers[i] = byteCharacters.charCodeAt(i);
   }
   const byteArray = new Uint8Array(byteNumbers);
   const blob = new Blob([byteArray], { type: 'application/pdf' });

   // Write the file
   this.file
     .writeFile(folderPath, fileName, blob, {
       replace: true,
     })
     .then(() => {
       this.presentToast('File downloaded successfully.');
       console.log('File downloaded successfully.');
     })
     .catch((error) => {
       this.presentToast('Error while downloading the file:');
       console.error('Error while downloading the file:', error);
     });
 }

Environment, Platform, Device

Version information

Cordova Version: 12.0.0

Checklist

  • I included all the necessary information above
  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions