Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
Sending large strings from Blazor Server via IJSRuntime to Javascript fails

(I am using Blazor Server running .Net 5, Google Chrome)

To be able to save a file and make it available for the user in the browser, via 'downloaded files', I have learned that you need to use some javascript.

To start with, I am from the server side of Blazor preparing a file, which comes from a WebAPi as a FileStream object.

 using (MemoryStream ms = new MemoryStream())
    {
            fsrResponse.Data.FileStream.CopyTo(ms);
            byteArray = ms.ToArray();
    }

 res= await js.SaveAs(fileName, byteArray);

I convert the File Stream to a Byte Array which is then passed to the SaveAs Task, which calls the javascript function after converting the byteArray to a Base64 string.

 public static async Task<bool> SaveAs(this IJSRuntime js, string filename, byte[] byteArray)
        {
            bool ret;

            try
            {
                string bytesBase64 = Convert.ToBase64String(byteArray);

                ret = await js.InvokeAsync<bool>("saveAsFile", TimeSpan.FromMinutes(1) ,filename, bytesBase64);
            }
            catch (Exception ex)
            {
                
                string message = ex.Message;
                ret = false;
            }

            return ret;
        }

This is the javascript method, which works most of the time.

function saveAsFile(filename, bytesBase64) {

    var ret;

    try {

        var a = document.createElement('a');

        a.download = filename;
        a.target = '_self';
        a.href = "data:application/octet-stream;base64," + bytesBase64;
        
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);

        ret = true;

    } catch (e) {

        ret = false;
    }

    return ret;
}

This workflow works perfectly for files that are smaller than 40-50 MB, but after that, the web site crashes. While debugging this, I can see that everything works until I call this line of code, after that, it stops executing. It doesn't hit the javascript code at all.

 ret = await js.InvokeAsync<bool>("saveAsFile", TimeSpan.FromMinutes(1) ,filename, bytesBase64);

I am not sure if this is related to file size directly, or if it is a timeout problem. (it takes around 33-34 seconds when I just checked, before it fails.

Anyone with some brilliant ideas? enter image description here

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda