user11328958
user11328958

Reputation:

How to get files from virtual directory and display in view Asp.net core

In the past I used to store and get my file from wwwroot folder and display them in the view, but now I would like to put all my file in a different file server location because of the size. below is the old implementation.

public IActionResult Index ()
        {
            string[] filePaths = Directory.GetFiles(Path.Combine(this.Environment.WebRootPath, "Audio/"));

            List<FileViewModel> files = new List<FileViewModel>();
            foreach (string filePath in filePaths)
            {
                files.Add(new FileViewModel { FileName = Path.GetFileName(filePath) });
            }

            return View(files);
                                
        }

Now that I want to move all my files from wwwroot because of size to another network location, I need to create a virtual directory and loop in the directory to get my files but I can't do it, I did read some articles that in Asp.net Core we need to have the following code in startup.cs in order to get to file from another location.

app.UseFileServer(new FileServerOptions
        {
            FileProvider = new PhysicalFileProvider(@"\\xxx"),
            RequestPath = new PathString("/Audio"),
            EnableDirectoryBrowsing = true
        });

I did above but not sure how I can use it in my controller to do the same I used in the past and display files in view index. any help would be appreciated. thanks

Upvotes: 1

Views: 3482

Answers (2)

user11328958
user11328958

Reputation:

Thank you everyone for replies, the reason I wanted to put my files in wwwroot and later on to somewhere that is local to webserver was because those where Audio files and browsers were only able to play Audio files that are coming from web server not local or network storage.

at first I tried Audio tag as below in html,then chrome blocked it,

so I was looking for something that webserver can serve my Audio file that chrome doesn't block it and the solution was virtual directory.

finally I did both by adding below to my start up and pointing my Audio tag to it

Startup

app.UseFileServer(new FileServerOptions
        {
            FileProvider = new PhysicalFileProvider(@"\\xxx"),
            RequestPath = new PathString("/Audio"),
            EnableDirectoryBrowsing = true
        });

Controller

string[] filePaths = Directory.GetFiles(Path.Combine(@"\\xxx));

View

<audio controls src="https://mywebserver/Audio/name.wav" type="audio/wav"> </audio>

I am not sure if I have done right, but so far working ok. if you you have any suggestion would be great.

Upvotes: 2

King King
King King

Reputation: 63347

IWebHostEnvironment (known as IHostingEnvironment from asp.net core 2.2 backwards) has 2 convenient properties to hold the root paths. One for wwwroot can be accessed through IWebHostEnvironment.WebRootPath, the other for content root (the parent folder of wwwroot) can be accessed through IWebHostEnvironment.ContentRootPath. So when you use another folder to hold your files, it should be at the same level with wwwroot and of course rooted from IWebHostEnvironment.ContentRootPath.

So the code can be changed to something like this:

 string[] filePaths = Directory.GetFiles(Path.Combine(this.Environment.ContentRootPath, 
                                                      "YourCustomRoot", "Audio/"));

The static files middlewares are used only to auto-serve requests for static files. So it does not play any roles here because you write code explicitly in your services to access the files.

Upvotes: 0

Related Questions