MVC 在视图中显示文件夹中的文件
我想要做的是在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只需要文件名,则可以将 Linq 查询更改为
然后(假设默认项目模板)将以下内容添加到 Index.cshtml 视图,
该视图将显示文件名列表
If you only want the file names then you can change your Linq query to
Then (assuming the default project template) add the following to the Index.cshtml view
Which will display the list of file names
FileInfo
对象,还是只检索文件路径?如果后者为 true,则只需将IEnumerable
返回到视图(而不是IEnumerable
,这就是您在上面代码中所做的事情)。提示:只需向 Linq 表达式添加一个Select
调用...FileInfo
object, or only a file path? If the latter is true, just return aIEnumerable<string>
to the view (instead of aIEnumerable<FileInfo>
, which is what you're doing in the above code). Hint: just add aSelect
call to your Linq expression...这是剃刀视图的简化示例。它将在 HTML 表中输出您的文件名。
This is a simplified example of a razor view. It will ouput your file names in a HTML table.