asp.net core下载文件支持配置,上传文件访问支持下载

Posted 棉晗榜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net core下载文件支持配置,上传文件访问支持下载相关的知识,希望对你有一定的参考价值。

  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        
            if (env.IsDevelopment())
            
                app.UseDeveloperExceptionPage();
            
            else
            
                app.UseExceptionHandler("/Error");
            
 

            string export_excel = MyConfigReader.GetConfigValue("export_excel");
            string export_word = MyConfigReader.GetConfigValue("export_word");
            string dir1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, export_excel);
            if (!System.IO.Directory.Exists(dir1))
            
                System.IO.Directory.CreateDirectory(dir1);
            
            string dir2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, export_word);
            if (!System.IO.Directory.Exists(dir2))
            
                System.IO.Directory.CreateDirectory(dir2);
            

            //没有地址则创建。 
            string upload1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploadImg");
            string upload2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upload");
            if (!System.IO.Directory.Exists(upload1))
            
                System.IO.Directory.CreateDirectory(upload1);
            
            if (!System.IO.Directory.Exists(upload2))
            
                System.IO.Directory.CreateDirectory(upload2);
            

            //提供上传的文件访问支持
            app.UseStaticFiles(new StaticFileOptions()
            
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(
                    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, export_excel)),
                RequestPath = "/" + export_excel,// 配置的虚拟路径映射
                ServeUnknownFileTypes = true
            );
            //提供上传的文件访问支持
            app.UseStaticFiles(new StaticFileOptions()
            
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(
                    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, export_word)),
                RequestPath = "/" + export_word,
                ServeUnknownFileTypes = true
            );

            //上传的图片,提供下载访问
            app.UseStaticFiles(new StaticFileOptions()
            
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(upload1),
                RequestPath = "/uploadImg",
                ServeUnknownFileTypes = true
            );
            app.UseStaticFiles(new StaticFileOptions()
            
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(upload2),
                RequestPath = "/upload",
                ServeUnknownFileTypes = true
            );

            app.UseStaticFiles();

            app.UseSession();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            
                //endpoints.MapRazorPages();
                endpoints.MapControllerRoute(
                   name: "default",
                   pattern: "controller=Home/action=index/id?");
            );

        
    

以上是关于asp.net core下载文件支持配置,上传文件访问支持下载的主要内容,如果未能解决你的问题,请参考以下文章

在ASP.NET Core 中上传文件

在 ASP.NET Core 3.1 中上传和下载大文件?

在 ASP.NET Core 中上传文件

ASP .NET Core从零到壹 || Swagger配置

[Asp.Net Core]Autofac支持配置文件

[Asp.Net Core]Autofac支持配置文件