Skip to content

Unable to use file read with server-side preview6 #54

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

Closed
tmarkovski opened this issue Jul 31, 2019 · 3 comments
Closed

Unable to use file read with server-side preview6 #54

tmarkovski opened this issue Jul 31, 2019 · 3 comments

Comments

@tmarkovski
Copy link

tmarkovski commented Jul 31, 2019

Describe the bug
Can't use the file reader component with server side Blazor. The signalr connection fails. On the backend side, the code just hangs when attempting to read from the stream at await stream.ReadAsync
Due to other dependencies with the project, I'm unable to use net 3.0 preview7 at this time.

To Reproduce
Steps to reproduce the behavior:
Pretty much using the suggested code to reproduce this.

public async Task<string> GetImagePathAsync()
{
    foreach (var file in await FileService.CreateReference(FileElement).EnumerateFilesAsync())
    {
        var fileInfo = await file.ReadFileInfoAsync();
        using var stream = await file.OpenReadAsync();
        var imageData = new byte[stream.Length];
        await stream.ReadAsync(imageData, 0, (int)stream.Length);

        using var imageStream = new MemoryStream(imageData);
        return await Client.UploadImageAsync(imageStream, fileInfo.Name, fileInfo.Type);
    }
    return null;
}

Project type
Server-side

Environment

  • Browser: Safari, Firefox and Chrome
  • Version of Blazor.FileReader - 0.12.19186
  • Version of .net sdk - 3.0.100-preview6-012264
@tmarkovski
Copy link
Author

Closing this, as I figured it out. The issue was the buffer size limit of 4096 when reading the stream. Working code below in case anyone runs into again.

public async Task<string> GetImagePathAsync()
{
    foreach (var file in await FileService.CreateReference(FileElement).EnumerateFilesAsync())
    {
        using var stream = await file.OpenReadAsync();
        var fileInfo = await file.ReadFileInfoAsync();
        var imageData = new byte[stream.Length];

        int count;
        var buffer = new byte[4096];
        while ((count = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0)
        {
            Array.Copy(buffer, 0, imageData, stream.Position - count, count);
        }

        using var imageStream = new MemoryStream(imageData);
        return await Client.UploadImageAsync(imageStream, fileInfo.Name);
    }
    return null;
}

@Tewr
Copy link
Owner

Tewr commented Aug 1, 2019

@tmarkovski great that you found a solution.

However, I think your code is equivalent to just calling
var imageStream = await file.CreateMemoryStreamAsync(4096);

In preview 7, using the solutions posted here, should work, but if you're stuck in preview6 solutions above will have to do.

@tmarkovski
Copy link
Author

I was not aware of that method. Thank you!

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

2 participants