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

  • Question

  • 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!

    • Moved by CoolDadTx Monday, April 30, 2018 1:43 PM ASP.NET related
    Sunday, April 29, 2018 3:24 PM

All replies

  • Have you tried to specify the name (the third parameter) without the folder?

       return File(…, …, "Articulos.xlsx");

    Check this place too: https://forums.asp.net/.

     

    Monday, April 30, 2018 5:05 AM
  • It's URI encoding for a '/'  -- %2F.

    https://msdn.microsoft.com/en-us/library/6196h3wt%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Monday, April 30, 2018 6:23 AM
  • Please post questions related to web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 30, 2018 1:43 PM