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

"Synchronous reads are not supported" when writing a stream #27

Open
becdetat opened this issue Mar 19, 2023 · 0 comments
Open

"Synchronous reads are not supported" when writing a stream #27

becdetat opened this issue Mar 19, 2023 · 0 comments

Comments

@becdetat
Copy link

I'm using Blazor to upload files. This version of my upload method fails:

    public async Task UploadFail(IBrowserFile file)
    {
        using var db = await _dbFactory.GetDatabase();
        var stream = file.OpenReadStream(file.Size);
        await db.FileStorage.UploadAsync("test", file.Name, stream);
    }

The call stack is (trimmed):

LiteDB.Async.LiteAsyncException: LiteDb encounter an error. Details in the inner exception.
       ---> System.NotSupportedException: Synchronous reads are not supported.
         at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.Read(Byte[] buffer, Int32 offset, Int32 count)
         at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)
         at LiteDB.LiteStorage`1.Upload(TFileId id, String filename, Stream stream, BsonDocument metadata)
         at LiteDB.Async.LiteStorageAsync`1.<>c__DisplayClass11_0.<UploadAsync>b__0()
         at LiteDB.Async.LiteDatabaseAsync.<>c__DisplayClass36_0`1.<EnqueueAsync>g__Function|0()

If I change it to use an intermediary MemoryStream it works, but I have a feeling that this would buffer the entire file into memory a second time. Here's my workaround though:

var stream = new MemoryStream();
await file.OpenReadStream(file.Size).CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
await db.FileStorage.UploadAsync(image.Id.ToString(), image.Filename, stream);
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

1 participant