public JsonResult GetCourseInitCover() { //設置相對路徑 string imgurl = Server.MapPath("~/Content/images"); //根據相對路徑,讀取所有文件,並且不查找子文件夾。之后篩選格式是圖片的文件 var strs = System.IO.Directory.GetFiles(imgurl, "*.*", System.IO.SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".jpg") || s.EndsWith(".gif") || s.EndsWith(".bmp") || s.EndsWith(".png")); List<KeyName> filelist = new List<KeyName>(); foreach (string file in strs) { KeyName item = new KeyName(); //讀取文件信息 System.IO.FileInfo fi = new System.IO.FileInfo(file); if (fi.Extension == ".jpg" || fi.Extension == ".gif" || fi.Extension == ".bmp" || fi.Extension == ".png") { item.Name = fi.Name; filelist.Add(item); } } //拼裝返回參數 var result = from dir in filelist select new { key = "/Content/images/" + dir.Name, value = dir.Name.Split('.')[0] }; return Json(result, JsonRequestBehavior.AllowGet); }
