KristofferStrube/Blazor.FileSystemAccess

How to read file bytes?

Closed this issue · 4 comments

Hello,

I've been trying to use your wrapper to read recursively files in a folder structure. I need to get all bytes from every file in an array, but I cannot imagine how to do it.

Is there anyone here that knows how to get all the content of a file in an array of bytes?

Thank you!

Hey @cityadpro,

There is only implemented a limited set of wrapper methods for the Blob class as that is not the core purpose of the library.
You can see the full set of methods here: https://w3c.github.io/FileAPI/#dfn-Blob

I have though made it possible to call the methods you wish on the Blob through JSInterop.
From a File we surface the underlying IJSObjectReference so you can use this to call any method on this like so:

File file; // Your file

bytes[] blobAsData = await file.JSReference.InvokeAsync<byte[]>("arrayBuffer");

Hello @KristofferStrube,

thank you very much for answering. I'll try your proposal as soon as I can.

Thank you!

Hello Kristoffer, hope you are doing well.

I have the exact same requirement as cityadpro and using the code your provided we get an exception even with the simplest example of just one file trying to get its content

    public async Task<byte[]> FileContentAsync()
    {
        return await JSReference.InvokeAsync<byte[]>("arrayBuffer");
    }

We added that method right below the text one in your library and call it from a file object from your library and get the following exception:
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: JSON serialization is attempting to deserialize an unexpected byte array.. See InnerException for more details.

Seems to not be returning a byte array at all.

If we set the type to dynamic and check what we get we don't see any useful properties. Are we missing something? did you test that approach actually works?

Any pointer would be great.
Thanks in advance!

Hey @mattmoll,
Sorry for the long response.

You are right. I didn't get around to testing my recommended solution for the problem but expected that it helped as there was no response.

I have dug more into the problem and found my mistake. I thought that the ArrrayBuffer was able to be serialized into a byte array as it is directly convertible to an Uint8Array.

The problem is that in order to convert an ArrayBuffer into a Uint8Array we need to parse it through the constructor. We can't do that through JS Interop directly yet. This is an issue, which is tracked in dotnet/aspnetcore#31151.
If you would give that an upvote then there is a bigger chance that they will work on implementing that soon.

Instead, you would need to make a wrapper function in JS that

  1. Takes the JSReference of the file.
  2. Calls the arrayBuffer method on the file.
  3. Creates a new Uint8Array from the arrayBuffer.
  4. Returns the Uint8Array which should be equivalent to a byte array.

I have not tested the above approach but would expect it works.