【问题标题】:Display only one image in a div在一个 div 中只显示一张图片
【发布时间】:2012-01-30 23:26:32
【问题描述】:

我想显示从文件夹中循环播放的图像。

<div class="blah">
<% System.IO.FileInfo[] files = new System.IO.DirectoryInfo(Server.MapPath("/MyPath/"))
.GetFiles();
var exefiles = from System.IO.FileInfo f in files
               where f.Extension == ".jpg" ||f.Extension == ".jpeg" 
               || f.Extension == "JPG" 
               select f;
foreach (System.IO.FileInfo f in exefiles) { %>
   <img src="blahblah.jpg" />
<% } %>
</div>

问题:div 显示文件夹中存储的所有图像。

但我只想在我的 div 中显示 1 张图片。

非常感谢。

【问题讨论】:

  • 如果文件夹中有多个jpg,如何确定显示哪一个?
  • 您想在单独的 div 中显示每张图片吗?

标签: asp.net css asp.net-mvc


【解决方案1】:

应该可以用Directory.GetFiles Method (String, String) (System.IO)做到这一点

类似:

<div class="blah">
<% 
    string path = "images/";
    string[] files = System.IO.Directory.GetFiles(Server.MapPath(path), "*jp*g");
    if (files.Length > 0) { %>
    <img src="<%= path + System.IO.Path.GetFileName(files[0])%>" />
<%  } %>
</div>

【讨论】:

  • 感谢您的回答,但我放置这些块时出错。这是错误The best overloaded method match for 'System.IO.Path.GetFileName(string)' has some invalid arguments
  • 听起来您传入的值由于某种原因不是字符串,在我的示例中 files[0] 是一个字符串,您是否进行了一些调整?
【解决方案2】:

您的视图中不应有任何登录,这就是它被称为模型视图控制器的原因。将您的逻辑放在 Action 中并将其传递给模型或 ViewBag 中的视图。

【讨论】:

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