Skip to content

filewriter always throws error code 2  #577

@NAMUS09

Description

@NAMUS09

Bug Report

when writing file in this path " window.cordova.file.externalRootDirectory + 'Download' " always throws error in filewriter.onerror. Issue not there in cordova v11, cordova-android v11 and cordova-plugin-file v7.

Problem

What is expected to happen?

It should write the file and return success.

What does actually happen?

throws error code 2

Information

tying to save pdf in download folder. I tried updating the plugin file to v8. Still issue is there.

Command or Code

private writeToFile(pdfData: BlobPart, fileName: string): Promise {
return new Promise((resolve, reject) => {
var storageLocation = '';

  switch (window.cordova.platformId) {
    case 'android':
      storageLocation = window.cordova.file.externalRootDirectory + 'Download';
      break;

    case 'ios':
      storageLocation = window.cordova.file.documentsDirectory;
      break;
  }

  var folderPath = storageLocation;
  console.log(folderPath);

  window.resolveLocalFileSystemURL(
    folderPath,
    (directoryEntry: any) => {
      directoryEntry.getFile(
        fileName,
        { create: true },
        (fileEntry: any) => {
          fileEntry.createWriter(
            (fileWriter: any) => {
              fileWriter.onwriteend = () => {
                resolve('success');
                console.log('File written successfully.');
              };

              fileWriter.onerror = (error: any) => {
                console.log('Error writing file: ');
                reject(error);
              };

              const dataObj = new Blob([pdfData], { type: 'application/pdf' });
              console.log(dataObj);

              fileWriter.write(dataObj);
            },
            () => {
              reject('Error creating file writer.');
              console.log('Error creating file writer.');
            }
          );
        },
        () => {
          reject('Error getting file entry.');
          console.log('Error getting file entry.');
        }
      );
    },
    () => {
      reject('Error resolving file system URL.');
      console.log('Error resolving file system URL.');
    }
  );
});

}

Environment, Platform, Device

while targeting to api 33 issue occurs. There is no issue in cordova v11 ,cordova-android v11 and cordova-plugin-file v7 targeting to api 32.

Version information

Cordova CLI v12,
Cordova-android v12
Cordova v8, v7 (both tested)

Checklist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions