I have a method that generates a file, this method is actually generating the file, I have another method inside my service that returns the file for download and for this I return a List until then I return that list with bytes for my controler as follows. Controller Call
public JsonResult RetornaDownloadExcel(Guid GuidExcel)
{
var retorno =
JsonServiceHelper.GimmeInstance().ExecuteArray("http://localhost:1234/",
"/SNService/RetornaDownloadExcel", GuidExcel, true);
List<Byte> Bytes = retorno.Value.ToObject<List<Byte>>();
var base64 = Convert.ToBase64String(Bytes.ToArray());
var fileSrc = String.Format("data:application/vnd.ms-excel;base64,{0}", base64);
return Json(new { Bytes });
}
that refers to this call.
public List<Byte> RetornaDownloadExcel(Guid GuidExcel)
{
List<Byte> Bytes = new List<Byte>();
String fullpath = "C:/Relatorio/Sns/" + GuidExcel + ".xls";
if (File.Exists(fullpath))
{
StreamReader sr = new StreamReader(fullpath);
MemoryStream ms = new MemoryStream();
sr.BaseStream.CopyTo(ms);
foreach (var Byte in ms.ToArray())
{
Bytes.Add(Convert.ToByte(Byte));
}
}
return Bytes;
}
and to call the controller I have a js with the following code
function RetornaDownloadExcel() {
jQuery.ajaxSettings.traditional = true;
$.ajax({
type: "POST",
url: '/SN/RetornaDownloadExcel',
data: { GuidExcel: guidexcel },
success: function (data) {
a = document.createElement('a');
document.body.appendChild(a);
var byteArray = new Uint8Array(data.Bytes);
blob = new Blob([byteArray], { type: 'application/vnd.ms-excel'
});
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = "Sns.xls";
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
},
error: function (xhr, status, error) {
}
});
};
But I can not download the excel file I think I'm missing something on the controller, can you help me with my error?
Thank you.