我按照本指南在目录中上传文件 https://www.learnrazorpages.com/razor-pages/forms/file-upload
我已将文件添加到“ wwwroot / files /”路径中
我想使用for循环将表中目录中的所有文件名打印出来。
问题是我不知道如何在asp .net Core中实现这一目标。
这就是我发布文件的方式
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'SMS 2 Server',
color: Colors.white,
theme: ThemeData(
//brightness: Brightness.light,
primarySwatch: primaryWhite,
primaryColor: Colors.blue,
inputDecorationTheme: InputDecorationTheme(
)
),
home: HomePage(),
);
我看到了一个如何访问文件提供程序的示例
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");
}
尽管我在目录内容中看不到我的文件
wwwroot / files上有3个子文件夹
我可以使用调试器查看它们,但是我无法访问它们的内容以查看其中有多少文件可以打印
我忘了添加,在启动时我添加了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);
}
请注意,我的文件位于wwwroot / files / subfolder1等中。