MVC 在视图中显示文件夹中的文件

发布于 2024-10-18 01:27:52 字数 1100 浏览 2 评论 0原文

我想要做的是在我的 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);
        }

任何帮助将不胜感激,谢谢:)

What I'm looking to do, is display the contents of a folder which is located on my server in a View in my MVC application.

I have what I think should be in place for the Action, however I'm a little unsure how to go about implementing the corresponding view and I was wondering if someone could point be in the right direction on that. (and also, if someone thinks my Action could be improved, advice would be welcome :) )

Here is the action:

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);
        }

Any help would be appreciated, thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南薇 2024-10-25 01:27:52

如果您只需要文件名,则可以将 Linq 查询更改为

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

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

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

该视图将显示文件名列表

If you only want the file names then you can change your Linq query to

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

Then (assuming the default project template) add the following to the Index.cshtml view

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

Which will display the list of file names

︶葆Ⅱㄣ 2024-10-25 01:27:52
  1. 恕我直言,您应该只公开视图真正需要的内容。想一想:您真的需要检索整个 FileInfo 对象,还是只检索文件路径?如果后者为 true,则只需将 IEnumerable 返回到视图(而不是 IEnumerable,这就是您在上面代码中所做的事情)。提示:只需向 Linq 表达式添加一个 Select 调用...
  2. 然后您的视图将渲染该模型 - 您需要的是一个 foreach 循环和一些 HTML 代码来完成它。
  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.
別甾虛僞 2024-10-25 01:27:52

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

  @model IEnumerable<FileInfo>

    <h1>Files</h1>
    <table>

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

    </table>

This is a simplified example of a razor view. It will ouput your file names in a HTML table.

  @model IEnumerable<FileInfo>

    <h1>Files</h1>
    <table>

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

    </table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文