如何使用Razor MVC显示文件夹中的图像? [英] How to show images from a folder using Razor MVC?

查看:67
本文介绍了如何使用Razor MVC显示文件夹中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Razor MVC,我想显示〜/Content/uploads"文件夹中的图像. 我想出了以下解决方案:

I am using Razor MVC and I would like to show images from "~/Content/uploads" folder. I came up with the following solution:

@foreach (FileInfo fileInfo in (new DirectoryInfo(Server.MapPath("~/Content/uploads"))
                                    .GetFiles().Where(x => x.Extension == ".jpg"))) {
    <img src="/@fileInfo
               .FullName
               .Substring(Server.MapPath("~/").Length)
               .Replace("\\", "/")"
         width="100">
}

代码的问题是我正在使用完整的文件路径 并且我要删除Server.MapPath()前缀.

The problem with the code is that I am taking the full file path and I am removing the Server.MapPath() prefix.

如何简化此代码?

推荐答案

您可以使用

这篇关于如何使用Razor MVC显示文件夹中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆