【问题标题】:Asp.Net Core 3.1 - Get Image from directory and display in razor view?Asp.Net Core 3.1 - 从目录中获取图像并在剃刀视图中显示?
【发布时间】:2021-10-30 15:22:09
【问题描述】:

如何从目录中获取图像并显示它?

“我看到了类似的帖子,但没有帮助”

我这样做了,但是没有用

控制器:

     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>

                    }

结果:

【问题讨论】:

    标签: html asp.net image asp.net-core razor-pages


    【解决方案1】:

    如下更改您的代码:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多