public ActionResult Avatar(Material model)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files.Count != 1)//如果文件列表为空则返回
                    return View();
                var file = Request.Files[0];//只上传第一个文件
                //根据日期生成服务器端文件名
                string uploadFileName = model.Id + Path.GetExtension(file.FileName);
                //生成服务器端绝对路径
                string absolutFileName = Server.MapPath("~/") + "UserUpload/Avatar/" + uploadFileName;
                //执行上传
                if (System.IO.File.Exists(absolutFileName))
                {
                    System.IO.File.Delete(absolutFileName);
                }
                if (!Directory.Exists(Path.GetDirectoryName(absolutFileName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(absolutFileName));
                }
                file.SaveAs(absolutFileName);
                model.Name = uploadFileName;
                //添加Material记录
                db.Materials.Attach(model);
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                //保存更改
                db.SaveChanges();
                return RedirectToAction("Index", new { Message = ManageMessageId.OperationSuccess });
            }

            ViewBag.Error = "存在错误,请检查输入。";
            return View();
        }
 //5 Views
 #region 上传文件模块
 // GET: Materials
 public ActionResult Materials(int page = 0)
 {
     IListPage test = new Material();
     var model = new ListPage<Material>(db.Materials.Where(m => m.Type == MaterialType.Activity || m.Type == MaterialType.Download || m.Type == MaterialType.Slider), page, pageSize); //实现分页功能
     return View(model);
 }
        public static Material Create(string description, MaterialType type, HttpPostedFileBase file, BaseDbContext db)
        {
            if (!type.Match(file))
                return null;
            string uploadFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-" + Guid.NewGuid() + Path.GetExtension(file.FileName);
            string absolutFileName;
            switch (type)
            {
                case MaterialType.Identity:
                    absolutFileName = HttpContext.Current.Server.MapPath("~/UserUpload/") + "Identity/" + uploadFileName;
                    break;
                case MaterialType.Avatar:
                    absolutFileName = HttpContext.Current.Server.MapPath("~/UserUpload/") + "Avatar/" + uploadFileName;
                    break;
                case MaterialType.Management:
                    absolutFileName = HttpContext.Current.Server.MapPath("~/UserUpload/") + "Management/" + uploadFileName;
                    break;
                default:
                    absolutFileName = HttpContext.Current.Server.MapPath("~/UserUpload/") + "Administrator/" + uploadFileName;
                    break;
            }

            if (!Directory.Exists(Path.GetDirectoryName(absolutFileName)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(absolutFileName));
            }
            //执行上传
            if (File.Exists(absolutFileName))
            {
                File.Delete(absolutFileName);
            }
            file.SaveAs(absolutFileName);
            Material material = new Material(uploadFileName, description, type);
            //添加Material记录
            db.Materials.Add(material);
            //保存更改
            db.SaveChanges();
            return db.Materials.Find(material.Id);
        }
Esempio n. 4
0
        //5 Views
        #region 上传文件模块
        // GET: Materials
        public ActionResult Materials(int page = 0)

        {
            IListPage test = new Material();
            var model = new ListPage<Material>(db.Materials, page, pageSize); //实现分页功能
            return View(model);
        }