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);
}
}