Feature: Support non-text file reading
See original GitHub issueHi @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:
- Created a year ago
- Comments:6 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry, I forgot to check those closed issues.
Your solution works perfectly, my code is as follows:
file.js
Index.razor
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
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 haswriting
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 extendingStream
withreading
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.