简体   繁体   English

如何从虚拟目录中获取文件并在视图中显示 Asp.net 内核

[英]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.过去我曾经从 wwwroot 文件夹存储和获取我的文件并将它们显示在视图中,但现在我想将我的所有文件放在不同的文件服务器位置,因为它的大小。 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.现在我想将所有文件从 wwwroot 移动到另一个网络位置,我需要创建一个虚拟目录并在目录中循环以获取我的文件,但我做不到,我确实阅读了一些文章Asp.net Core 我们需要在 startup.cs 中有以下代码才能从另一个位置获取文件。

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.我在上面做了,但不确定如何在我的 controller 中使用它来执行我过去使用的相同操作并在视图索引中显示文件。 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. IWebHostEnvironment (从asp.net core 2.2向后称为IHostingEnvironment )有 2 个方便的属性来保存根路径。 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 .一个用于wwwroot可以通过IWebHostEnvironment.WebRootPath访问,另一个用于内容根( wwwroot的父文件夹)可以通过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 .因此,当您使用另一个文件夹来保存文件时,它应该与wwwroot处于同一级别,当然也应该以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. static 文件中间件仅用于自动处理对 static 文件的请求。 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.如果您要从网络共享中获取文件,请记住从需要提供真实凭据的网络访问文件,最好的凭据将是 web 应用程序的应用程序池标识,或者如果正在使用 windows 身份验证,那么您可以使用要求。

access network drive 访问网络驱动器

Once you got access to the file you can render files using FileResult or FileStreamResult or PhysicalFileResult访问文件后,您可以使用 FileResult 或 FileStreamResult 或 PhysicalFileResult 呈现文件

render files in asp.net core 在 asp.net 内核中渲染文件

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.谢谢大家的回复,我想把我的文件放在 wwwroot 的原因,然后放在网络服务器本地的某个地方是因为那些音频文件和浏览器只能播放来自 web 服务器的音频文件不是本地的或网络存储。

at first I tried Audio tag as below in html,then chrome blocked it,起初我在 html 中尝试了如下音频标签,然后 chrome 阻止了它,

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.所以我一直在寻找网络服务器可以提供我的音频文件的东西,chrome 不会阻止它,解决方案是虚拟目录。

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 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.如果你有任何建议会很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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