我有两个图像文件夹,即a和b,它们包含产品图像。所有图像都保存为productid。怎么能通过那里找到文件夹中的图像 制作并将图像保存在列表中。
Dim AllImages As New List(Of String)
保存后,如何将这些图像逐个渲染到字符串构建器中,如
Dim imagecontainer As New StringBuilder
For Each image In AllImages
imagecontainer.Append("<img src="image" alt="" />") Next image
Any ideas of how to do this, your help will be highly appreciated.
答案 0 :(得分:2)
您可以在页面代码隐藏中使用ResolveClientUrl:
var clientImageUrl = ResolveClientUrl("yourimagefolder/sampleimage.jpg");
或者您可以使用将返回相同结果的VirtualPathUtility.ToAbsolute:
VirtualPathUtility.ToAbsolute("yourimagefolder/sampleimage.jpg")
为了读取文件夹中的所有文件,然后按文件名(您的ID)过滤它们,您可以使用(使用System.IO命名空间):
DirectoryInfo directory = new System.IO.DirectoryInfo("c:\YourFolder");
var allFiles = directory.GetFiles(".", System.IO.SearchOption.AllDirectories);
var fileFounds = from file in allFiles
where file.Name == "YourID"
select file;
foreach (var file in fileFounds)
{
//Build your image
}
编辑:这是vb.net版本(我使用过代码转换器,希望它运行良好:)
'Using ResolveClientUrl
Dim clientImageUrl = ResolveClientUrl("yourimagefolder/sampleimage.jpg")
'Using VirtualPathUtility
VirtualPathUtility.ToAbsolute("yourimagefolder/sampleimage.jpg")
'Reading files from directory
Dim directory As DirectoryInfo = New System.IO.DirectoryInfo("c:\YourFolder")
Dim allFiles = directory.GetFiles(".", System.IO.SearchOption.AllDirectories)
Dim fileFounds = _
Where file.Name = "YourID"
'Build your image
For Each file As var In fileFounds
Next
答案 1 :(得分:1)
或者,您可以使用绑定到图像路径字符串列表的Repeater。这样可以更好地控制标记。
以下是使用Repeater的示例。
标记:
<asp:Repeater id="imageRepeater" runat="server">
<ItemTemplate>
<img src='<%# Container.DataItem %>' alt="" />
</ItemTemplate>
</asp:Repeater>
代码隐藏:
imageRepeater.DataSource = imageFiles
imageRepeater.DataBind()