question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: Support non-text file reading

See original GitHub issue

Hi @KristofferStrube, I’ve recently had some issues reading non-text files into the browser.

Expect an API like this:

var handle = await directory.GetFileHandleAsync("sample.db");

var file = await handle.GetFileAsync();

var buffer = new byte[file.Size];

file.Read(buffer, 0, buffer.Length);

or like this:

var handle = await directory.GetFileHandleAsync("sample.db");

var file = await handle.GetFileAsync();

var reader = new FileReader(file);

var buffer = new byte[reader.Size];

reader.Read(buffer, 0, buffer.Length);

Thank you for your work!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
fixnilcommented, Oct 5, 2022

Sorry, I forgot to check those closed issues.

Your solution works perfectly, my code is as follows:

file.js

export async function arrayBuffer(blob) {

  var buffer = await blob.arrayBuffer();

  var bytes = new Uint8Array(buffer);

  return bytes;
}

Index.razor

var file = await _handle.GetFileAsync();

var module = await this.JSRuntime.InvokeAsync<IJSObjectReference>("import", "./file.js");

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

await System.IO.File.WriteAllBytesAsync(Filename, bytes);

I don’t know if I need to submit a PR as an alternative, before the official File API goes live? The code looks like this:

Blazor.FileSystemAccess/src/KristofferStrube.Blazor.FileSystemAccess/Blob/Blob.cs

// ...
public class Blob
{
    // ...

    public async Task<byte[]> ArrayBufferAsync()
    {
        return await helper.InvokeAsync<byte[]>("arrayBuffer", JSReference);
    }
}
1reaction
KristofferStrubecommented, Oct 4, 2022

Hi @fixnil, 😊 Thanks for the issue.

This is related to issue #10 which is closed; you can read more about it there. As mentioned in the first response to that issue the File System Access API doesn’t give a way to read a file as a Uint8Array. A Blob can be read as an ArrayBuffer but that is not directly convertible to a byte array. So to do this you would need to call a JS method through JSInterop and go through a couple of steps as described in the last comment of that thread.

In release 1.1.0 I added the possibility to stream to a FileSystemWritableFileStream by having it extend Stream that has writing enabled. I would like it to be possible to do the same for Blob in order to be able to stream from it by again extending Stream with reading enabled.

I am unsure whether this is inside this library’s intended scope as it is a lot of work with the JS Blob which is a part of the File API. A full wrapper of the File API should probably be its own library so that it can be used by other libraries/projects that would use that functionality without depending on this library.

I am leaving this issue open so that I can track interest in solving this. I haven’t decided whether or not this should be a part of this library or potentially spun out to its own library (Blazor.File) that this library (Blazor.FileSystemAccess) could reference.

I have noted a couple of libraries that also work with JSInterop and Files which might be a help for future work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Open a Non-Text File in Python
With Python, we can read and write binary files using standard functions. For instance, we can use the open() function to create a...
Read more >
reading non-text file in c++
1 Answer 1 ... Like all other non-text files, mp3 files don't contain lines so you shouldn't use std::getline . Use istream::read and...
Read more >
Reading and Writing to text files in Python
Read Only ('r') : Open text file for reading. The handle is positioned at the beginning of the file. If the file does...
Read more >
fopen() — Open Files
Open a text file in append mode for reading or updating at the end of the file. The fopen() function creates the file...
Read more >
Reading text files with readtext
Currently, readtext supports plain text files (.txt), data in some form of JavaScript Object Notation (.json), comma-or tab-separated values (.csv, .tab, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found