ericpap
ericpap

Reputation: 2937

Downloaded file from folder includes %2F char on .NET Core

This must be a dumb question. I have a web API Rest on c# that have an endpoint to generate and download an excel file. I use NPOI library.

It works fine if the file is generated within the root of my site. But when I try to move my file to a folder, file name include folder and de %2F char. I suppose this is an URL encode problem, but don't know how to fix it. This is my code.

[AllowAnonymous]
    [HttpGet("ExportExcel")]
    public async Task<IActionResult> ExportExcel(int activos = 1, int idarticulo = -1, string filtro = "", string ordenar = "", int ordenarsentido = 1)
    {
        var memory = new MemoryStream();
        var newFile = @"Export/Articulos.xlsx";

        //IArticulosService articulosService = new ArticulosService(_validation.sConnectionString, _validation.sEmpresa);
        var mycon = _validation.GetConnectionStringFromClientID("ClientId1");
        IArticulosService articulosService = new ArticulosService(mycon, "Ciatema");

        try
        {
            // Genero el excel con el listado
            articulosService.ExportarExcel(newFile, activos, filtro, ordenar, ordenarsentido);
            using (var stream = new FileStream(newFile, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;
            return File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", newFile);
        }
        catch (QueryFormatException ex)
        {
            return BadRequest(ex.Message);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex);
            return StatusCode(StatusCodes.Status500InternalServerError);
        }
    }

Donwload filename is: Export%2FArticulos.xlsx when i need it to be only Articulos.xlsx. Thanks!

Upvotes: 0

Views: 37

Answers (1)

kumar chandraketu
kumar chandraketu

Reputation: 2370

you should be passing file name only instead of complete path in your return File method like below

  return File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Path.GetFileName(newFile));

Upvotes: 1

Related Questions