如何在MVC应用程序中显示文件夹中的任何图像或第一张图像

时间:2018-12-04 15:39:37

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

如何在我的asp.net mvc应用程序的文件夹中显示任何图像或第一张图像

现在我正在这样使用

<img src="~/UserProfileImages/Images/100.jpg" class="img-responsive" alt="" />

但是我想显示文件夹中没有文件名或扩展名的第一张图像...文件夹中有很多图像。

1 个答案:

答案 0 :(得分:1)

leaflet(data = quakes[1:20,]) %>% 
  addProviderTiles(providers$OpenStreetMap.Mapnik) %>%
  addTiles(urlTemplate = "", attribution = 'I did this, you hear?! Also Leaflet.')

然后在您的视图中引用图像源(在此示例中,视图为Index.cshtml):

    public class SomeController
    {
        public ActionResult Index()
        {
            var myModel = new ImageModel();
            var imagePaths = ReadPhotosFromDirectory();

            if(!imagePaths.IsNullOrEmpty())
            {
                //get the first file path from the directory
                myModel.ImageSrc = imagePaths[0];
            }

            return View(myModel);
        }

        //this method assumes all files in the directory are images.
        //you should check the file extensions as well to be safer
        private string[] ReadPhotosFromDirectory()
        {

            string[] fileLocations = Directory.GetFiles("\\your\\folder\\path").ToArray();
            return fileLocations;
        }
    }