.net Core实战简单文件服务器

  首先新建一个ASP.NET Core 项目,选中空的模板,以下图所示浏览器

image.png

在NuGet包中添加Microsoft.AspNetCore.StaticFilesapp

image.png

添加好之后咱们在Startup.cs中添加对应的中间件(若是不明白中间件是什么查看上一篇)async

 

默认文件夹是wwwroot 若是不想使用wwwroot须要使用StaticFileOptions来设置目录。ide

这里设置一下目录到项目根目录ui

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)      
{
            loggerFactory.AddConsole();
            var staticfile = new StaticFileOptions();
            staticfile.FileProvider = new PhysicalFileProvider(@"F:\xm\core\XzyFilesServer\XzyFilesServer\bin\Debug\netcoreapp1.1\");
            app.UseStaticFiles(staticfile);//使用默认文件夹wwwroot
            if(env.IsDevelopment())
            {           
                app.UseDeveloperExceptionPage();
            }   
            app.Run(async (context) =>{
                await context.Response.WriteAsync("HelloWorld!");
            });
}

这里咱们设置到项目根目录this

而后在根目录建立一个readme.txt文件 spa

image.png

在里面写入.net core.net

image.png

而后程序运行起来访问咱们的文件能够看见 成功浏览readme.txt文件code

若是咱们须要浏览全部的文件夹及文件,就须要用到UseDirectoryBorwser ,具体以下视频

public void ConfigureServices(IServiceCollection services)     
{
            services.AddDirectoryBrowser();    
}
       
// This method gets called by the
runtime. Use this method to configure the HTTP request pipeline.
      
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)      
{
            loggerFactory.AddConsole();
            var dir = new DirectoryBrowserOptions();
            dir.FileProvider = new PhysicalFileProvider(@"F:\xm\core\XzyFilesServer\XzyFilesServer\bin\Debug\netcoreapp1.1\");
            app.UseDirectoryBrowser(dir);
            var staticfile = new StaticFileOptions();
            //制定目录也能够指定其余文件夹
            staticfile.FileProvider = new PhysicalFileProvider(@"F:\xm\core\XzyFilesServer\XzyFilesServer\bin\Debug\netcoreapp1.1\");
            //若是不指定使用默认文件夹wwwroot
            app.UseStaticFiles(staticfile);
            if(env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("HelloWorld!");
            });
        }

这里须要在Startup的ConfigureServices方法中加入services.AddDirectoryBrowser();

image.png

程序运行起来后,结果如上图

咱们在换成C盘试一试

 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)      
{
            loggerFactory.AddConsole();
            var dir = new DirectoryBrowserOptions();
            dir.FileProvider = new PhysicalFileProvider(@"C:\");
            app.UseDirectoryBrowser(dir);
            var staticfile = new StaticFileOptions();
            //制定目录也能够指定其余文件夹
            staticfile.FileProvider = new PhysicalFileProvider(@"F:\xm\core\XzyFilesServer\XzyFilesServer\bin\Debug\netcoreapp1.1\");
            //若是不指定使用默认文件夹wwwroot
            app.UseStaticFiles(staticfile);
            if(env.IsDevelopment())
            {               
                app.UseDeveloperExceptionPage();     
            }
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("HelloWorld!");
            });
}

image.png

能够看到,咱们能够访问C盘的全部文件夹及文件了。

当打开一些文件时会发现,有些文件能够打开,有些文件会出现404错误,那是由于MIME Type没有识别出来。

         咱们能够手动设置这些MIME Type,也能够给这些未识别的设置一个默认值。具体代码以下:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)       
{
            loggerFactory.AddConsole();
            var dir = new DirectoryBrowserOptions();
            dir.FileProvider = new PhysicalFileProvider(@"C:\");
            app.UseDirectoryBrowser(dir);
            var staticfile = new StaticFileOptions();
            //制定目录也能够指定其余文件夹
            staticfile.FileProvider = new PhysicalFileProvider(@"C:\");
            staticfile.ServeUnknownFileTypes = true;
            staticfile.DefaultContentType = "application/x-msdownload";//设置默认MIMEType
            //若是不指定使用默认文件夹wwwroot
            app.UseStaticFiles(staticfile);
            if(env.IsDevelopment())
            {         
                app.UseDeveloperExceptionPage();
            }
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("HelloWorld!");
            });
}

设置好后 对于未识别的,默认浏览器会下载这些文件。

若是想用手机查看电脑的图片或者视频等文件,则在加入一句代码就能够实现了。

public class Program  
{     
    public static void Main(string[] args)      
    {
             var host = new WebHostBuilder().UseKestrel()
              .UseContentRoot(Directory.GetCurrentDirectory())
              .UseIISIntegration()
              .UseUrls("http://*:5000")//加上这个就能经过ip+端口访问了
              .UseStartup<Startup>()
              .UseApplicationInsights()
              .Build();
              host.Run();       
    }
}

image.png

手机访问效果图如上

相关文章
相关标签/搜索