[英]How to store a list of files from disk into an array in C# ASP.NET?
In my site, I need to read all files from a particular directory, store them into a list or some object, and output them to the page so that when the user clicks on them they are downloaded. 在我的站点中,我需要从特定目录中读取所有文件,将它们存储到列表或某个对象中,然后将它们输出到页面,以便在用户单击它们时将它们下载。 Meaning, I will display the filename of the file on the page as a hyperlink, and when the user clicks on it they will be prompted to download whatever they clicked on. 意思是,我将在页面上以超链接的形式显示文件的文件名,并且当用户单击该文件名时,将提示他们下载其单击的内容。
I am using ASP.NET MVC with C#. 我正在将ASP.NET MVC与C#一起使用。 I imagine my model would be something like: 我想我的模型应该是这样的:
public class PageViewModel
{
public string Username {get; set; }
//public List FileList {get; set; } ??
}
Then in my controller I would actually execute the getting of the files: 然后在我的控制器中,我将实际执行文件的获取:
public ViewResult SomePage(PageViewModel vm)
{
vm.Username = "AlbatrossCafe";
//loop or something that iterates over all files in the folder
//{
// vm.SomeList.push(currentFile);
//}
}
Then in my view I would render them out. 然后在我看来,我将它们渲染出来。 That part is easy. 这部分很容易。
I am having a problem with figuring out which object to store the files in as well as how to actually read them from the disk. 我在确定要存储文件的对象以及如何实际从磁盘读取文件时遇到问题 。
Let's just say I need to read all files from a folder on my desktop in location: C:\\Users\\Default\\Desktop\\file_folder
. 假设我需要从位于以下位置的桌面上的文件夹中读取所有文件: C:\\Users\\Default\\Desktop\\file_folder
。 What object should I use and how can I read files into that object from this folder? 我应该使用哪个对象,以及如何从该文件夹中将文件读入该对象?
Things to keep in mind: 注意事项:
Your model should look something like this. 您的模型应如下所示。 Because you only need a name and a URL for your display. 因为您只需要显示的名称和URL。
Public class Resource
{
Public string URL {get; set;}
Public string Name {get;set;}
}
Create a list of these and Bind the Name to your value and the URL to the HREF to your display elements. 创建一个这样的列表,并将“名称”绑定到您的值,并将“ HREF”的URL绑定到显示元素。
edit As far as reading the names from the file your controller should loop through each folder and pull the name and path like this 编辑只要从文件中读取名称,控制器就应该遍历每个文件夹并像这样拉出名称和路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.