Error in Chrome when downloading Excel created by ReportViewer

2

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 the 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 I have no problems. The weird thing is that it works for me in Firefox.

I would like to know what I should do to avoid that problem.

How do I get it to work for me in Google Chrome?

Maybe something should be enabled in web.config . I would like to be able to at least reproduce the error.

Image of network transport in Mozilla:

In Google Chrome:

    
asked by Danilo 16.09.2016 в 15:51
source

1 answer

0

I found the solution, I assumed that in the function downloadReport had some problem, therefore change the way of doing it. So I did it:

 public FileResult GeneraReporte(string format)//el format es "EXCEL"
    {
        string nombreDataSource="DataSet_1";

        string nombreReporte = "Report.rdlc";

        LocalReport report = new LocalReport();
        report.ReportPath = Server.MapPath("~/Reportes/" + nombreReporte);

        report.DataSources.Clear();
        ReportDataSource reportDataSource = new ReportDataSource();
        reportDataSource.Value = query();
        reportDataSource.Name = nombreDataSource;            
        report.DataSources.Add(reportDataSource);

        report.Refresh();

        return GetFileContentResult(report, format, null, "TestReport");


    public FileContentResult GetFileContentResult(Report report, String format, String deviceInfo, String fileDownloadName)
    {
        String mimeType;
        String encoding;
        String filenameExtension;
        String[] streamIds;
        Warning[] warnings;

        FileContentResult fileContentResult = new FileContentResult(report.Render(format, deviceInfo, out mimeType, out encoding, out filenameExtension, out streamIds, out warnings), mimeType);
        fileContentResult.FileDownloadName = Path.ChangeExtension(fileDownloadName, filenameExtension);

        return fileContentResult;
    }

The only thing that I would have left with doubt, what happens when it is for pdf, the format should be equal to PDF ??

    
answered by 17.09.2016 / 17:33
source