html - Asp.Net Core 3.1 - 从目录中获取图像并在剃刀视图中显示?
问题描述
如何从目录中获取图像并显示它?
“我看到了类似的帖子,但没有帮助”
我这样做了,但没有用
控制器 :
string[] filePaths = Directory.GetFiles(Path.Combine(_hostingEnv.WebRootPath, @"site\img\banner\"));
ViewBag.ListImgMainDefalt = filePaths;
看法 :
foreach (var item in ViewBag.ListImgMainDefalt)
{
<tr>
<td>
<img src="@item" />
</td>
</tr>
}
解决方案
更改您的代码,如下所示:
string[] filePaths = Directory.GetFiles(Path.Combine(_hostingEnv.WebRootPath, @"site\img\banner\"));
List<string> fileName = new List<string>();
foreach (string filePath in filePaths)
{
fileName.Add(@"/site/img/banner/"+Path.GetFileName(filePath));
}
ViewBag.ListImgMainDefalt = fileName;
推荐阅读
- angular - 带角度的 Chrome 扩展:无法运行测试
- javascript - 这个正则表达式可以简化吗?所需的模式是大写的单词
- python - 使用 iconphoto 方法时 Tkinter 滞后
- python-3.x - Pycharm中未使用的导入语句“import lxml”
- python-3.x - sagemaker 端点 content_type 无法设置属性
- php - CodeIgniter 登录功能表单不起作用
- javascript - 为什么继承在javascript中没有标准方式,为什么它有不同的定义方式以及每种方式的方面是什么?
- json - 是否可以使用 NIFI 中的 Jolt 规范从单个 JSON 创建多个 JSON 对象
- julia - Optim: InexactError: Int64(0.01) 使用 IPNewton 时
- java - 具有一对多的 Hibernate (JPA) 继承