Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store Images other than wwwroot folder

I stored Images In a folder name "Images" which is not in wwwroot folder. So now i am unable to access it so my question is that How to give access to folder which contain images and thats folder is not the sub-folder of wwwroot? Or Its compulsory to put these files in wwwroot folder?

like image 644
Ahmad Avatar asked May 10 '17 13:05

Ahmad


1 Answers

Yep Asp.Net Core provides a way to do that. You can serve images from an Images directory which is not in the wwwroot folder. In fact, you can serve them from anywhere including embed resource files or even out of a database.

The key is to register a FileProvider in the Configure method of the Startup.cs file so that Asp.Net Core knows how to access the file to be served.

So for example, in your case since you want to serve images from a directory called Images, let's say your directory hierarchy looks like this:

wwwroot
     css
     images
     ...

 Images
     my-image.png

To serve up my-image.png from Images you could use the following code:

 public void Configure(IApplicationBuilder app){
     app.UseStaticFiles(); // For the wwwroot folder

     app.UseStaticFiles(new StaticFileOptions(){
     FileProvider = new PhysicalFileProvider(
         Path.Combine(Directory.GetCurrentDirectory(), @"Images")),
         RequestPath = new PathString("/Images")
     });
 }

You can learn more about serving static files here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files

like image 61
RonC Avatar answered Sep 28 '22 02:09

RonC