循环读取文件夹下的文件

今天做一个功能,需要实现读取一个文件夹的内容,根据文件的日期进行排序,取出并绑定在页面上面。研究了很久,看到人家写的有点启发。
HTML:
<table cellspacing="0">
        <thead>
            <tr>
                <th>标题th>
                <th>发件人th>
                <th class="thead-lasttd">日期th>
            tr>
        thead>

        <tbody id="trAnnouncement">tbody>
    table>

Jquery :

  <script>
          $(function () {
            var _fileAddress = ehr.servicesUrl + "Content1/Announcement/";
            var trAnnouncement="";
            //动态加载菜单
            ehr.ajax(ehr.servicesUrl + 'get/Announcement', data, function (json) {
                if (json.Success)
                {
                    var datas = json.Value.announcement;
                    for (var i = 0; i < datas.length; i++) {
                        trAnnouncement += "" + datas[i].FirstName + "";
                        trAnnouncement += "总务部 " + datas[i].LastTime + " ";
                    }
                }
                else
                {
                    trAnnouncement += "  ";
                }
                $("#trAnnouncement").html(trAnnouncement);
            });
        });
    script>

后台代码;

   public ActionResult getAnnouncement()
        {
            JsonRult result = new JsonRult();

            string _fileAddress = Server.MapPath("~/Content1/Announcement");

            var directory = new DirectoryInfo(_fileAddress);
            string FirstName = "";
            string LastTime = "";
            string json = "{\"announcement\":[";
            foreach (var item in directory.GetFileSystemInfos().OrderByDescending(x => x.LastWriteTime))
            //OrderByDescending 升序  OrderBy降序   x.排序条件
            {
                if (item.Extension.ToLower() != ".files")
                { 

                    FirstName = item.Name.Substring(0, (item.Name.LastIndexOf(".")));//标题
                    LastTime = item.LastWriteTime.ToString("yyyy-MM-dd");//发布日期
                    json += "{\"FirstName\":\"" + FirstName + "\",\"Name\":\"" + item.Name + "\",\"LastTime\":\"" + LastTime + "\"},";  

                }

            }
            json = json+"]}";
            result.Value = json; 
            result.Message = "";
            result.Success = (FirstName != "" ? true : false);
            var rultResult = Json(result, JsonRequestBehavior.AllowGet);
            return rultResult;
        }

显示的效果
循环读取文件夹下的文件_第1张图片

你可能感兴趣的:(Js,mvc,html,css,json)