写在前面
上篇文章实现了图片列表,这篇文章实现文档列表将轻车熟路,因为逻辑基本相似,只是查询条件的不同。这里将txt,doc,docx,ppt,pptx,xls,xlsx的文件都归为文档列表中。
系列文章
[实战]MVC5+EF6+MySql企业网盘实战(23)——文档列表
实现
代码与图片列表基本相同,这里在考虑是否使用一个模板,不过想想,为了以后为每种类型的文件列表做扩展,还是单独放在一个controller中了。再仔细想想有没有更好的实现方式。这里就先这样来做了。
前端代码
@{ ViewBag.Title = "Lists"; Layout = "~/Views/Shared/_Layout.cshtml";}
服务端代码
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Script.Serialization;using Wolfy.NetDisk.BLL;using Wolfy.NetDisk.IBLL;using Wolfy.NetDisk.Model;using Wolfy.NetDisk.Site.Models;namespace Wolfy.NetDisk.Site.Controllers{ public class DocumentController : Controller { private IMyFileServiceRepository _myFileServiceRepository = new MyFileServiceRepository(); private ILogServiceRepository _logServiceRepository = new LogServiceRepository(); private IFileTypeServiceRepository fileTypeServiceRepository = new FileTypeServiceRepository(); // // GET: /Images/ public ActionResult Lists() { UserInfo user = Session["user"] as UserInfo; if (user == null) { return RedirectToAction("Login", "UserInfo"); } return View(); } [HttpGet] public JsonResult GetDocuments() { UserInfo userInfo = Session["user"] as UserInfo; int page = Convert.ToInt32(Request.Params["page"]); if (page <= 0) { page = 1; } if (userInfo == null) { RedirectToAction("Login", "UserInfo"); } int pageSize = 10; int recordCount = 0; var documentPaged = _myFileServiceRepository.FindPaged(page, pageSize, out recordCount, x => x.User.Id == userInfo.Id && x.IsDelete == false && (x.FileIcon.Contains("DocType.png") || x.FileIcon.Contains("XlsType.png") || x.FileIcon.Contains("PptType.png") || x.FileIcon.Contains("PdfType.png") || x.FileIcon.Contains("TxtType.png")), false, x => x.CreateDt); int totalPage = Convert.ToInt32(Math.Ceiling(recordCount * 1.0 / pageSize)); List lstMyFileViewModel = new List (); foreach (var item in documentPaged) { lstMyFileViewModel.Add(new MyFileViewModel() { Id = item.Id, FileIcon = item.FileIcon, FileServerUrl = "/NetDisk/" + item.FileMd5 + item.FileExt, Name = item.Name, FileThumnailUrl = string.Empty, Size = item.FileSize, Dt = item.CreateDt }); } return new JsonResult() { Data = new JavaScriptSerializer().Serialize(new { _data = lstMyFileViewModel, _code = 200, total = totalPage }), JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } }}
测试
总结
实现起来还是比较简单的,马上就2016年了,祝大家元旦快乐。