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

Filesystem improvements #974

Open
joel-daros opened this issue Nov 18, 2018 · 13 comments
Open

Filesystem improvements #974

joel-daros opened this issue Nov 18, 2018 · 13 comments

Comments

@joel-daros
Copy link

joel-daros commented Nov 18, 2018

  • The FileWriteOptions directory property needs to accept string concatenations like FilesystemDirectory.ExternalStorage + '/downlad', not only types defined in Enum.

  • Will be useful If the The FileWriteOptions directory accepts Blob type not only strings.

  • When you write a file, if it already exists, it will be overwritten? Is there any parameter for this?

@vially
Copy link
Contributor

vially commented Nov 18, 2018

👍 Using Blobs for file read/writes would be really useful.

@nikosdouvlis
Copy link
Contributor

Hello, are there any plans to implement any of the improvements suggested above?

@oliverandersencox
Copy link

@jcesarmobile is there any progress with saving as a blob? If not, what would you recommend is the best approach to store blob data?

@mlynch
Copy link
Contributor

mlynch commented May 19, 2019

Would be a good one to look at after 1.0 to improve filesystem performance, along with #1392 and #1391

@pnaylor
Copy link

pnaylor commented Nov 6, 2019

Also feeling pretty limited without Blob writability currently.
Is the best option to just fall back to cordova-plugin-file for that usage for now?

@black-hawk85
Copy link

Sharing my workaround with you.

I'm using web-implementation of the Filesystem plugin.
Don't know if it's a good solution but it helps solving my problem.

import { FilesystemDirectory, FilesystemEncoding } from '@capacitor/core'
// const { Filesystem } = Plugins // => Doesn't work with blob in native
// Workaround: Use Web-Implementation of plugin
import { FilesystemPluginWeb } from '@capacitor/core/dist/esm/web/filesystem.js'
const Filesystem = new FilesystemPluginWeb()

@digaus
Copy link
Contributor

digaus commented Mar 4, 2020

Any news on blob support?

@digaus
Copy link
Contributor

digaus commented Mar 12, 2020

If anyone still needs this, I implemented a solution myself which works with current cap version. Here is a sample app:

https://github.com/digaus/CapacitorSaveBlob

Tested myself in browser, android and on iOS.

Maybe @jcesarmobile or someone else is willing to integrate this into the current filesystem plugin (maybe add a new encoding type blob) if this solution is up to their standard 😄

Edit: with this PR it will also work for electron -> #2567

@mishrasatyam
Copy link

Any update on this in version 3?

@qliqdev
Copy link

qliqdev commented Jun 25, 2021

Capacitor V3

Trying to safe Fetched PDF file. But when i open the file it outputs blank page

Preview: pdfOut.pdf

My Code:

const reader = new FileReader();
reader.addEventListener('loadend', (e) => {
  const text = e.target.result;
  this.writeFile(text, 'title.pdf').then();
});
reader.readAsBinaryString(myBlobPdfFile);
async writeFile(str, title) {
  const {publicStorage} = await Filesystem.checkPermissions();
  if (publicStorage === 'granted') {
    this.checkDir().then(async () => {
      const {uri} = await Filesystem.writeFile({
        path: 'pdf/' + title,
        data: str,
        directory: Directory.Documents,
        encoding: Encoding.UTF8,
      });
      if (uri) {
        /// action to share or open file
      }
    });
  } else {
    await Filesystem.requestPermissions();
  }
}

Here is result of reader

%PDF-1.3
%ºß¬à
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/MediaBox [0 0 263.6220472440945173 453.5433070866141634]
/Contents 4 0 R
>>
endobj
....
....
....
<<
/Size 28
/Root 27 0 R
/Info 26 0 R
/ID [ <FB056280F091518448E6503C4DC48D31> <FB056280F091518448E6503C4DC48D31> ]
>>
startxref
44017
%%EOF
@capacitor/core 3.0.2
@capacitor/cli 3.0.2
@capacitor/android 3.0.2
@capacitor/ios 3.0.2

@capacitor/filesystem (1.0.2

@Hodes
Copy link

Hodes commented Mar 15, 2022

It would be really useful specially because there is an issue wthin the FileReader which in this case can be used to convert a Blob to a base64 to be written using current Filesystem.writeFile.

The issue consists in not firing the onloadend, onload etc... on FileReader instance when using Capacitor. The thread: FileReader API not firing metions a workaround which is working fine for converting Blob to base64, then saving with Filesystem.

I'm watching this thread now looking forward to replace the current workaround with a final solution, hopefully saving the Blob instance directly.

Thanks !

@iamcco
Copy link

iamcco commented Jun 8, 2022

Any update?

@caiorsantanna
Copy link

It would be really useful specially because there is an issue wthin the FileReader which in this case can be used to convert a Blob to a base64 to be written using current Filesystem.writeFile.

The issue consists in not firing the onloadend, onload etc... on FileReader instance when using Capacitor. The thread: FileReader API not firing metions a workaround which is working fine for converting Blob to base64, then saving with Filesystem.

I'm watching this thread now looking forward to replace the current workaround with a final solution, hopefully saving the Blob instance directly.

Thanks !

Hi, a cannot comment on the mentioned issue anymore, so i will comment here so the next guys wont stuck anymore :)

-> If in web (for development) get normal FileReader instance
-> For native, get _realReader instance
-> Force angular to change detection because this will not work properly otherwise

private run() {
  const response = await fetch(path);
  const blob = await response.blob();
  const reader = this.getFileReader();

  reader.onload = (): void => {
    console.log(reader.result)
    this.changeDetectorRef.detectChanges();
  };

  reader.readAsDataURL(blob);
}

private getFileReader(): FileReader {
  if (!Capacitor.isNativePlatform()) {
    return new FileReader();
  }

  const fileReader = new FileReader() as any;
  const reader = fileReader._realReader;

  return reader;
}

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

No branches or pull requests