简体   繁体   English

如何将文件列表从磁盘存储到C#ASP.NET中的数组中?

[英]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: 注意事项:

  1. I don't need the actual text contents of the files 我不需要文件的实际文本内容
  2. Every file from the folder specified needs to be read 需要读取指定文件夹中的每个文件

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.

相关问题 如何在应用程序状态(C#ASP.net MVC)中存储列表〜数组 - How do I store List ~ Array in the Application State (C# ASP.net MVC) 如何从asp.net中的javascript访问ac#数组 - How to access a c# array from javascript in asp.net C#ASP.NET MVC项目和Google Maps:如何使用C#List &lt;&gt;中的值填充JavaScript数组 - C# ASP.NET MVC Project and Google Maps: How to fill JavaScript array with values from C# List<> 下载ASP.NET MVC C#字节数组列表中包含的多个文件 - Download multiple files contained in a list of byte array in ASP.NET MVC C# 商店标签如何进入C#asp.net列表应用程序? - How can a store tag's into a list C# asp.net application? 如何从asp.net中的数组中的多选下拉列表中存储选定值的列表? - How to Store List of selected value from multiselect dropdown list in array in asp.net? 如何在asp.net/c# datatable中存储超链接? - How to store hyperlink in asp.net/c# datatable? 在列表框中显示目录中的Word文件列表(Asp.net C#) - showing list of word files from a directory in the listbox (Asp.net c#) 如何使用C#而不使用ASP.net获取服务器中的文件列表 - how to get list of files in the server using C# not using ASP.net 如何将多个值从listview列存储到数据库asp.net的单个条目C# - how to store multiple values from listview column to a single entry to database asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM