cordova-plugin-file icon indicating copy to clipboard operation
cordova-plugin-file copied to clipboard

Issue with file download on iOS

Open kunalSBasic opened this issue 9 months ago • 4 comments

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

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

kunalSBasic avatar May 21 '24 15:05 kunalSBasic