Return Dowload Excel via MVC Controller

4

I have a method that generates an excel file and writes it to a directory, but then I get the directory path and need to download the excel file, but nothing happens, no error. Can someone help in what I'm failing?

[HttpGet]
    public FileResult ObterDadosClientesBDO(ParametrosListaCliente filtro)
    {
        var jsonResult = Json(InstanceResolverFor<IServicoListaClientesBDO>.Instance.ObterDadosClientesBDO(filtro).ToArray(), JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;

        string nomeRelatorio = string.Format("{0}{1}.xlsx","NOMERELATORIO", DateTime.Now.ToString("ddMMyyyyhhmmss"));
        string caminhoRelatorio = GerarRelatorioExcel(jsonResult, nomeRelatorio);


        return File(caminhoRelatorio, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", nomeRelatorio);
    }
    
asked by anonymous 29.05.2017 / 20:13

1 answer

1

As you did not give more details after my comment, follow what is probably happening.

On your% w / w < is not occurring error, probably because the object return is being return , it is still an error, but an Exception Handling occurs in this case and no exception is thrown because the application manages to "deal with the error" without harming the rest of the application's operation.

The null should be indicating the wrong path of the file.

You can still do a treatment like in the example below:

[HttpGet]
public FileResult ObterDadosClientesBDO(ParametrosListaCliente filtro)
{
    var jsonResult = Json(InstanceResolverFor<IServicoListaClientesBDO>.Instance.ObterDadosClientesBDO(filtro).ToArray(), JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;

    string nomeRelatorio = string.Format("{0}{1}.xlsx","NOMERELATORIO", DateTime.Now.ToString("ddMMyyyyhhmmss"));
    string caminhoRelatorio = GerarRelatorioExcel(jsonResult, nomeRelatorio);

    var arquivo = File(caminhoRelatorio, "application/vnd.ms-excel", nomeRelatorio);

    if(arquivo == null)
    {
        //se for nulo apresenta uma mensagem de aviso antes do return
        return arquivo;
    }
    else
    {
        //Se não apenas retorne o arquivo
        return arquivo;
    }
}

References:

30.05.2017 / 17:13