I have the following code in the view:
<% using (Html.BeginForm("GeneraReporte", "Libro"))
{ %>
<input id="format" runat="server" name="format" type="hidden" value="xls" />
<input type="image" src="<%:Url.Content("~/Images/iconos/export_excel.png")%>" alt="Exportar Excel" />
<% } %>
On Controller:
[HttpPost]
public ActionResult GeneraReporte()
{
try
{
String format = Request.Form["format"];
Reportes reportes = new Reportes();
DataTable lista_tabla = new DataTable();
String sql = "SELECT dato1, dato2 from empresa";
conexion.conectar();
MySqlDataAdapter datos = new MySqlDataAdapter(sql, conexion.con);
conexion.cerrar();
datos.Fill(lista_tabla);
string nombre_display = "Listado";
string nombreDataSource = "DataSet_Excel";
string formatoNameRender = "Excel";
string nameArchivoExcel = "Libro_Diario";
string nombreReporte = "Report_libro.rdlc";
reportes.solicitaReporte(format, nombre_display, lista_tabla, nombreDataSource, formatoNameRender, nameArchivoExcel, nombreReporte);
}
catch (Exception error)
{
}
return View();
}
In a class named Reports, I have the functions that are responsible for generating the Excel:
public void solicitaReporte(string format, string nombre_display, dynamic query, string nombreDataSource, string formatoNameRender, string nameArchivoExcel, string nombreReporte)
{
LocalReport report = new LocalReport();
report.DisplayName = nombre_display;
report.ReportPath = HttpContext.Current.Server.MapPath("~/Reportes/" + nombreReporte);
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Value = query;
reportDataSource.Name = nombreDataSource;
report.DataSources.Add(reportDataSource);
byte[] bytes = report.Render(formatoNameRender);
this.downloadReport(bytes, format, nameArchivoExcel);
//return System.Web.Mvc.Controller.File(bytes, format);
}
private void downloadReport(byte[] data, string format, string nameArchivoExcel)
{
Stream stream = new MemoryStream(data);
byte[] buffer = new Byte[10000];
int length;
long data_length;
try
{
data_length = stream.Length;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + nameArchivoExcel + "." + format);
while (data_length > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
length = stream.Read(buffer, 0, 10000);
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
buffer = new Byte[10000];
data_length = data_length - length;
}
else
{
data_length = -1;
}
}
}
finally
{
if (stream != null) { stream.Close(); }
HttpContext.Current.Response.Close();
}
}
But when I try to download in Google Chrome, it tells me
Error: Network error.
I download any other file from the internet and have no problems. The strange thing is that it works for me in Firefox.
I would like to know what I should do to avoid this problem.
How do I make it work for me in Google Chrome?
Maybe something needs to be enabled in the web.config
. I wish I could at least reproduce the error.
Image of network transport in Mozilla:
In Google Chrome:
I found the solution, I assumed that the downloadReport function had some problem, so I changed the way of doing it. I did it like that:
The only thing that would leave me with doubt, what happens when it is for pdf, the format should be equal to PDF?