Search code examples
c#signalrblazor

Getting big data through SignalR - Blazor


I have a component library that uses JS code to generate an image as a base64 string and the image needs to be transposed to C#. The image size is larger than MaximumReceiveMessageSize.

Can I get the value of the MaximumReceiveMessageSize property in C#? I need a way to correctly split the picture into chunks, or some other way to transfer it.

My component can be used in a Wasm or Server application. I can't change the value of the MaximumReceiveMessageSize property.

Thanks


Solution

  • Using a stream as described in the Stream from JavaScript to .NET solved my problem.

    From Microsoft docs:

    In JavaScript:

    function streamToDotNet() {
        return new Uint8Array(10000000);
    }
    

    In C# code:

    var dataReference = await JS.InvokeAsync<IJSStreamReference>("streamToDotNet");
    using var dataReferenceStream = await dataReference.OpenReadStreamAsync(maxAllowedSize: 10_000_000);
    var outputPath = Path.Combine(Path.GetTempPath(), "file.txt");
    using var outputFileStream = File.OpenWrite(outputPath);
    await dataReferenceStream.CopyToAsync(outputFileStream);
    

    In the preceding example: JS is an injected IJSRuntime instance. The dataReferenceStream is written to disk (file.txt) at the current user's temporary folder path (GetTempPath).