Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cordova file plugin not working with Capacitor #1620

Closed
strogof opened this issue Jun 1, 2019 · 13 comments
Closed

Cordova file plugin not working with Capacitor #1620

strogof opened this issue Jun 1, 2019 · 13 comments

Comments

@strogof
Copy link

strogof commented Jun 1, 2019

the code below works without problems in ionic 4
in capacitor I get always err 5 encoding_err as result when calling this.file.writeFile
I tried with Filesystem api but no blob, only string in data options (no luck with (#31 (comment))
the community plugin Downloader does not work
last but not least, why, ionic team announce Capacitor release 1.0 and here you say is still in Beta?
anyway, thanks for the great work done so far

          const req = new HttpRequest(
            'GET',
            urlDB, {
              responseType: 'blob',
              observe: 'events',
              reportProgress: true
            });
          this.http.request(req)
            .subscribe((event) => {
              switch (event.type) {
                case HttpEventType.DownloadProgress:
                  if (event.total) {
                    this.progressImportA = Math.round(100 * event.loaded / event.total);
                  } else {
                    this.progressImportA = 0
                  }
                  break;
                // handle the response event received
                case HttpEventType.Response:
                  // When getting the full response body
                  const contFile: any = event.body
                  console.log('ordini.db scaricato');
                  this.file.writeFile(
                    this.file.externalApplicationStorageDirectory,
                    'ordini.db',
                    contFile,
                    { replace: true }
                  )
                    .then(() => {
                      this.progressFaseA = 'Import Database OK';
                      console.log('install DB OK')
                    },
                    (error) => {
                      this.alert('write ordini.db', '', error.message)
                    });
                  break;
              }
            }
@jcesarmobile
Copy link
Member

the code below works without problems in ionic 4 in capacitor I get always err 5 encoding_err as result when calling this.file.writeFile

Can you provide a proper sample code that I can use on a blank project, or even better if you can provide a sample app?
I've made cordova file plugin work in the past, but as it's a very complex plugin I might have not tested every possible situation.

I tried with Filesystem api but no blob, only string in data options (no luck with (#31 (comment))

What explained on that comment should work, I've done similar things and worked, if you don't use an encoding then it's considered a base64 string, as long as your string is a valid base64 string it should work.
But you might be facing this issue on zone.js affecting ionic-native when used in Capacitor: #1564

last but not least, why, ionic team announce Capacitor release 1.0 and here you say is still in Beta?

I suppose you mean in the README. I removed it yesterday.

@strogof
Copy link
Author

strogof commented Jun 4, 2019

hi jcesarmobile,
thanks for the reply

Can you provide a proper sample code that I can use on a blank project, or even better if you can provide a sample app?

I hope I can give you a complete app for tomorrow,
I warn you when it's ready ...

What explained on that comment should work, I've done similar things and worked, if you don't use an encoding then it's considered a base64 string, as long as your string is a valid base64 string it should work.
But you might be facing this issue on zone.js affecting ionic-native when used in Capacitor: #1564

for me it is important to solve the problem with the file plugin, that I use a lot in my app, along with file opener and pdfmake ...

I suppose you mean in the README. I removed it yesterday.

you're right, and I've also seen the removal ..

sorry for my google translator english : ) and thanks again

@strogof
Copy link
Author

strogof commented Jun 5, 2019

https://github.com/strogof/capacitorFilePugin
the file to download is a 6 Mb sqlite3 db
I have to do the same with a zip file containing jpg images
the test with filewriter api, write the downloaded file but the size is 13 bytes

@jcesarmobile
Copy link
Member

OMG, I've spent countless hours trying to figure out what was wrong, and in the end, it was a problem with the clobbers and merges order which is already fixed by #1616

Thanks for the sample app. BTW, the Capacitor writeFile is not working because you are not correctly converting the Blob to base64 string

@strogof
Copy link
Author

strogof commented Jun 8, 2019

hi, thanks again
forgive me, what i have to do exactly?

@jcesarmobile
Copy link
Member

Wait for next release

@jcesarmobile
Copy link
Member

Oh, even with the fix it won't work in your app because of #1564
You'll have to use Capacitor Filesystem plugin to save the file.
To make it work, you need to remove cordova-plugin-file from the project as zone.js conflicts with it and makes FileReader not to work.
Then, to properly convert the Blob to base64 you can do it like this:

    const reader = new FileReader;
    reader.onload = () => {
        var result = reader.result as string;
        Filesystem.writeFile({
          data: result,
          directory: FilesystemDirectory.Data,
          path: 'ordiniFilesystem.db'
      })
      .then(() => {
          console.log('install ordiniFilesystem OK');
        });
    };
    reader.readAsDataURL(contFile);

@strogof
Copy link
Author

strogof commented Jun 9, 2019

ok, I can save my files
the problem now is with fileOpener plugin (file not found when I try to open the file)
another conflict with zone.js?
I updated the sample project...

@jcesarmobile
Copy link
Member

No, the problem is you are using FilesystemDirectory.Data, but that's not a path, it's just a constant to get the Filesystem type, you have to use Filesystem.getUri to get the file path and use that path with fileOpener

@strogof
Copy link
Author

strogof commented Jun 9, 2019

ok many thanks, but I think it is enough for me
everything is complicated and no help from doc
I just tried to add electron platform, that was my main interest
nothing worked and no doc
sorry, but I can't spend another week to understand how and if it works
anyway, you are a very kind person so excuse me for boring you with my problems
have a nice day
Luigi

@Sampath-Lokuge
Copy link

Sampath-Lokuge commented Aug 22, 2019

@strogof have you found out the way to work this on Electron? i.e. Filesystem.writeFile for me it gives Unable to write file Error: ENOENT: no such file or directory, open 'C:\Users\Sampath\Documents\secrets\text.txt'

But this plugin is working fine on Android platrrom. Any clue?

@strogof
Copy link
Author

strogof commented Aug 23, 2019 via email

@Sampath-Lokuge
Copy link

@strogof Yes, it is true. I also gave up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants