[译]ASP.NET Core 2.0 本地文件操作
问题
如何在ASP.NET Core 2.0中受限地访问本地目录和文件信息?
答案
新建一个空项目,修改Startup类,添加访问本地文件所需的服务:
public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IFileProvider>( new PhysicalFileProvider(Directory.GetCurrentDirectory())); }
创建一个中间件,读取根目录下的所有文件,输出文件名:
public class HelloFileProviderMiddleware { private readonly RequestDelegate _next; private readonly IFileProvider _fileProvider; public HelloFileProviderMiddleware(RequestDelegate next, IFileProvider fileProvider) { _next = next; _fileProvider = fileProvider; } public async Task Invoke(HttpContext context) { var output = new StringBuilder(""); IDirectoryContents dir = _fileProvider.GetDirectoryContents(""); foreach (IFileInfo item in dir) { output.AppendLine(item.Name); } await context.Response.WriteAsync(output.ToString()); } } public static class UseHelloFileProviderExtensions { public static IApplicationBuilder UseHelloFileProvider(this IApplicationBuilder app) { return app.UseMiddleware<HelloFileProviderMiddleware>(); } }
运行,此时页面效果:
当然,我们也可以通过IFileProvider接口读取单个文件信息:
public class HelloFileProviderMiddleware { private readonly RequestDelegate _next; private readonly IFileProvider _fileProvider; public HelloFileProviderMiddleware(RequestDelegate next, IFileProvider fileProvider) { _next = next; _fileProvider = fileProvider; } public async Task Invoke(HttpContext context) { IFileInfo file = _fileProvider.GetFileInfo("Startup.cs"); using (var stream = file.CreateReadStream()) { using (var reader = new StreamReader(stream)) { var output = await reader.ReadToEndAsync(); await context.Response.WriteAsync(output.ToString()); } } } }
运行,此时页面效果:
讨论
ASP.NET Core 2.0提供了继承自接口IFileProvider的PhysicalFileProvider类型,用于受限地访问本地文件系统,它是对System.IO.File的一个封装。
我们可以在Startup的Configure()方法内将IFileProvider配置为一个服务,然后通过构造函数注入添加到中间件或者控制器中。这样可以在一个地方控制文件的访问路径(也就是应用程序启动的时候)。
IFileProvider有两个重要的方法:
1. GetDirectoryContents:返回IDirectoryContents接口。可用于遍历某个目录中的全部文件或目录。
2. GetFileInfo:返回IFileInfo接口。通过其CreateReadSteam方法可以读取文件内容。
====start by sanshi=========================
下面通过递归来遍历根目录下全部文件和目录,修改中间件代码:
public class HelloFileProviderMiddleware { private readonly RequestDelegate _next; private readonly IFileProvider _fileProvider; public HelloFileProviderMiddleware(RequestDelegate next, IFileProvider fileProvider) { _next = next; _fileProvider = fileProvider; } public async Task Invoke(HttpContext context) { var output = new StringBuilder(""); ResolveDirectory(output, "", ""); await context.Response.WriteAsync(output.ToString()); } private void ResolveDirectory(StringBuilder output, string path, string prefix) { IDirectoryContents dir = _fileProvider.GetDirectoryContents(path); foreach (IFileInfo item in dir) { if (item.IsDirectory) { output.AppendLine(prefix + "[" + item.Name + "]"); ResolveDirectory(output, item.PhysicalPath.Substring(Directory.GetCurrentDirectory().Length), prefix + " "); } else { output.AppendLine(prefix + item.Name); } } } }
运行,此时页面效果:
====end by sanshi=========================
源代码下载
原文:https://tahirnaushad.com/2017/08/18/asp-net-core-file-providers/