Reputation: 485
I'm using MVC4 c#. I'm trying to load images that I need to show on a page dynamically by reading the content of the image folder and doing a foreach loop. I'm not sure how to read the content of the folder which is called ImageFiles which is located in the project and not the c:\ of the server. This is what I used and it works on my local computer but when I use ../../filename/filename/ImageFiles
as the path it does not work. Can anyone help?
string filePath = @"../../Content/EventFiles/ImageFiles";
DirectoryInfo directory = new DirectoryInfo(filePath);
@foreach (FileInfo file in directory.GetFiles())
{
<\a href="../../Content/EventFiles/ImageFiles/@file.Name">
<\img src="/Content/EventFiles/ImageFiles/@file.Name" />
<\/a>
}
Upvotes: 0
Views: 2572
Reputation: 485
Answer is Server.MapPath(). For desktop applications DirectoryInfo() works but for a web application I had to use Server.MapPath().
Thanks
Upvotes: 0
Reputation: 583
You are not referencing the filePath correctly.
Try this one on your View..
@{DirectoryInfo dir = new DirectoryInfo(Server.MapPath(Url.Content("~/Content/EventFiles/ImageFiles")));}
@foreach (var file in dir.GetFiles())
{
<img src="@Url.Content("~/Content/EventFiles/ImageFiles/" + file.Name)" />
}
Upvotes: 1