0

I followed this guide to upload files in directory https://www.learnrazorpages.com/razor-pages/forms/file-upload

I have added files into "wwwroot/files/" path

I want to print all the files names in the directory in a table using a for loop.

Problem is that i can't figure out how to achieve this in asp .net Core.

this is how i post files

   public async Task OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                if (Documento.Length > 2097152)
                {
                    _toastNotification.AddErrorToastMessage("O tamanho deste ficheiro é superior a 2MB. ");
                }                
                else
                {                    

                    var file = Path.Combine(_environment.ContentRootPath, "wwwroot\\files\\ficha-tecnica", _context.Referencias.Where(r => r.Id == ReferenciaId).FirstOrDefault().Nome + ".pdf");

                    Path.Combine(_environment.ContentRootPath, "wwwroot\\files\\ficha-tecnica");
                    using (var fileStream = new FileStream(file, FileMode.Create))
                    {
                        await Documento.CopyToAsync(fileStream);
                    }
                }
            }

            ViewData["ReferenciaId"] = new SelectList(_context.Referencias, "Id", "Nome");
        }

I saw an example how to access a fileprovider

        private readonly NoPaperContext _context;
    private readonly IHostingEnvironment _environment;
    private readonly IToastNotification _toastNotification;
    private readonly IFileProvider _fileProvider;

    public IndexModel(NoPaperContext context, IHostingEnvironment environment, IToastNotification toastNotification, IFileProvider fileProvider)
    {
        _context = context;
        _environment = environment;
        _toastNotification = toastNotification;
        _fileProvider = fileProvider;
    }

    [BindProperty]
    [Display(Name = "Referência")]
    [Required(ErrorMessage = "Selecione uma Referência")]
    public int ReferenciaId { get; set; }

    [BindProperty]
    [Required(ErrorMessage = "Selecione um Documento")]
    [RegularExpression(@"([a-zA-Z0-9\s_\\.\-:_()])+(.pdf)$", ErrorMessage = "Apenas são aceites ficheiros .pdf")]
    public IFormFile Documento { get; set; }

    public IDirectoryContents DirectoryContents { get; private set; }

    public void OnGet()
    {
        ViewData["ReferenciaId"] = new SelectList(_context.Referencias, "Id", "Nome");

        DirectoryContents = _fileProvider.GetDirectoryContents(string.empty);


    }

Though i can't see my files on the Directory Contents

I have 3 subfolders on the wwwroot/files

I can see them using debugger, but i can't access their contents to see how many files inside to print them

I forgot to add, in my startup i add fileprovider service

services.AddSingleton<IFileProvider>(
            new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/files")));

notice my files are in wwwroot/files/subfolder1 etc..

Jackal
  • 3,359
  • 4
  • 33
  • 78
  • updated with how i upload files – Jackal Jun 18 '19 at 13:08
  • What specifically are you struggling with – BugFinder Jun 18 '19 at 13:11
  • i have files in subfolders, wwwroot/files/subfolder1 etc.. in the post i updated i show how i get file provider instance, i can see the subfolder names using debugger, however i can't figure out how to access the contents or provide an extra path so i can print the files names inside it on a table. Also updated to show the url in startup service – Jackal Jun 18 '19 at 13:14
  • Possible duplicate of [How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac](https://stackoverflow.com/questions/43709657/how-to-get-root-directory-of-project-in-asp-net-core-directory-getcurrentdirect) – Owen Pauling Jun 18 '19 at 13:28

0 Answers0