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..