Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!