Reputation: 263
How can I take the image from the directory and display it?
"I saw similar posts but it did not help"
I did this but it didn't work
Controller :
string[] filePaths = Directory.GetFiles(Path.Combine(_hostingEnv.WebRootPath, @"site\img\banner\"));
ViewBag.ListImgMainDefalt = filePaths;
View :
foreach (var item in ViewBag.ListImgMainDefalt)
{
<tr>
<td>
<img src="@item" />
</td>
</tr>
}
Upvotes: 0
Views: 701
Reputation: 36645
Change your code like below:
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;
Upvotes: 1