简体   繁体   中英

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

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.

if you are looking to get files from network share then remember to access files from the network you need to provide authentic credentials, the best credentials will be the web application's app pool identity or if windows authentication is being used then you may use the identity of request.

access network drive

Once you got access to the file you can render files using FileResult or FileStreamResult or PhysicalFileResult

render files in asp.net core

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM