English 中文(简体)
MVC在视图中显示文件夹中的文件
原标题:MVC Display Files from a Folder in a View

我想做的是在MVC应用程序的视图中显示位于服务器上的文件夹的内容。

我有我认为应该为《行动》制定的内容,但我有点不确定如何实施相应的观点,我想知道是否有人能在这方面指出正确的方向。(此外,如果有人认为我的行动可以改进,欢迎提出建议:)

以下是操作:

public ActionResult Index()
        {
            DirectoryInfo salesFTPDirectory = null;
            FileInfo[] files = null;

            try
            {
                string salesFTPPath = "E:/ftproot/sales";
                salesFTPDirectory = new DirectoryInfo(salesFTPPath);
                files = salesFTPDirectory.GetFiles();
            }
            catch (DirectoryNotFoundException exp)
            {
                throw new FTPSalesFileProcessingException("Could not open the ftp directory", exp);
            }
            catch (IOException exp)
            {
                throw new FTPSalesFileProcessingException("Failed to access directory", exp);
            }

            files = files.OrderBy(f => f.Name).ToArray();

            var salesFiles = files.Where(f => f.Extension == ".xls" || f.Extension == ".xml");

            return View(salesFiles);
        }

如有任何帮助,我们将不胜感激,谢谢:)

最佳回答

如果您只想要文件名,那么您可以将Linq查询更改为

files = files.Where(f => f.Extension == ".xls" || f.Extension == ".xml")
  .OrderBy(f => f.Name)
  .Select(f => f.Name)
  .ToArray();
return View(files);

然后(假设为默认项目模板)将以下内容添加到Index.chtml视图中

<ul>
  @foreach (var name in Model) {
    <li>@name</li>
  }
</ul>

它将显示文件名列表

问题回答
  1. IMHO you should expose only what is really needed by the view. Think about it: do you really need to retrieve a whole FileInfo object, or only a file path? If the latter is true, just return a IEnumerable<string> to the view (instead of a IEnumerable<FileInfo>, which is what you re doing in the above code). Hint: just add a Select call to your Linq expression...
  2. Then your view will just render that model - what you need is a foreach loop and some HTML code to do it.

这是剃刀视图的简化示例。它将在HTML表中输出您的文件名。

  @model IEnumerable<FileInfo>

    <h1>Files</h1>
    <table>

    @foreach (var item in Model) {
        <tr>
            <td>
                @item.Name
            </td>
        </tr>
    }

    </table>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签