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

201
Visualizações
How to download a Data Base File type file in javascript and c#

I'm trying to download a file of type DataBaseFile which is a sqlite backup, by javascript. I send this file as a byte[] through a controller to javascript. However, the download is corrupted or wrong, with the file size being larger than the correct one. And if I save the same file through c# it saves just fine without errors. Does anyone have a solution?

I have the following code in C# to get a file of type DataBaseFile (.db) from the database and send it to javascript.

    var posBackup = PosBackupModel.Get(conexao, idEmpresa, dataHora);
    using (var memoryStream = new MemoryStream(posBackup.Backup))
      {                       
           var bytes = memoryStream.ToArray();
           return Json(
           new
           {
                nome = "Backup",
                backup = bytes 
           });
      }

Below is my javascript code to download the file:

function DownloadPosBackup(dataHora) {
    $.ajax(
    {
            url: '@Url.Action("DownloadPosBackup", "Empresa"),
            type: "GET",
            async: true,
            success: function (response) {
                blob = new Blob([response.backup], { type: "text/plain;charset=utf-8" });
                saveAs(blob, response.nome + ".db");
            }
    });

    return false;
}

But it downloads the corrupted (or wrong) file, comes with more KB than the correct one.

If I save the file through C# using the following code, it saves just fine, but I need it to be through javascript, does anyone have any solution?

var posBackup = PosBackupModel.Get(conexao, idEmpresa, dataHora);
using (var memoryStream = new MemoryStream(posBackup.Backup))
{
                    
    using (var fileStream = new FileStream("C:\\Users\\mathe\\Desktop\\teste\\test.db", FileMode.CreateNew, FileAccess.ReadWrite))
    {
        memoryStream.WriteTo(fileStream);
    }                       
}
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