Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda