public ResourceViewModel(Resource resource)
 {
     this.ID = resource.ID;
     this.ResourceName = resource.ResourceName;
     this.Time = Helper.Time.ToTimeTip(resource.UploadTime).ToString();
     this.Username = resource.User.UserName;
     this.UserID = resource.UserId;
 }
 public ResourceShowViewModel(Resource resource)
 {
     this.ID = resource.ID;
     this.ResourceName = resource.ResourceName;
     this.Time = resource.UploadTime.ToString();
     this.Username = resource.User.UserName;
     this.UserID = resource.UserId;
     this.Replies = resource.Replies.OrderByDescending(r => r.Ptime).ToList();
     this.Type = resource.Type;
     this.Description = resource.Description;
 }
        public ActionResult doUpload(ResourceUploadViewModel model, HttpPostedFileBase file,HttpPostedFileBase file1)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    Entity.User user = CurrentUser;
                    var fileName = Path.Combine(Request.MapPath("~/Upload"), DateHelper.GetTimeStamp() + Path.GetFileName(file.FileName));
                    try
                    {
                        file.SaveAs(fileName);
                        Resource resource = new Resource { ResourceName = model.ResourceName, Description = model.Description, TypeAsInt = model.ResourceType, UploadTime = DateTime.Now, Path = DateHelper.GetTimeStamp() + Path.GetFileName(file.FileName), UserId = user.ID };

                        if (file1 != null)
                        {
                            System.IO.Stream stream = file1.InputStream;
                            byte[] buffer = new byte[stream.Length];
                            stream.Read(buffer, 0, (int)stream.Length);
                            stream.Close();
                            resource.Icon = buffer;


                            db.Resources.Add(resource);
                            db.SaveChanges();
                            ResourceViewModel vResource = new ResourceViewModel(resource);

                            var context = GlobalHost.ConnectionManager.GetHubContext<ResourceHub>();//得到Signalr context 
                            context.Clients.All.GetNew(vResource);  //将新上传的资源广播到全部客户端
                            return RedirectToAction("Index", "Home");
                        }
                        else
                        {
                            ModelState.AddModelError("", "上传资源截图!");
                        }
                       
                    }
                    catch
                    {
                        ModelState.AddModelError("", "上传文件出错");
                    }
                }

                else
                {
                    ModelState.AddModelError("", "上传文件出错");
                }
            }
            else
            {
                ModelState.AddModelError("", "上传文件出错");
            }
            return View(model);
        } 
        /// <summary>
        /// 展示截图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public ActionResult ShowIcon(int id)
        {
            Resource resource = new Resource();
            resource = db.Resources.Find(id);
            return File(resource.Icon, "image/jpg");
        }
 /// <summary>
 /// 显示资源
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public async Task<ActionResult> Show(int id)
 {
     Resource resource = new Resource();
     resource = await db.Resources.FindAsync(id);
     ViewBag.resource = new ResourceShowViewModel(resource);
     return View();
 } 
 /// <summary>
 /// 删除
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public async Task<ActionResult> Delete(int id)
 {
     Resource resource = new Resource();
     resource = db.Resources.Find(id);
     db.Resources.Remove(resource);
     int result = await db.SaveChangesAsync();
     if (result > 0)
     {
         var path = Server.MapPath("~/Upload/" + resource.Path);
         System.IO.File.Delete(path);
         return Content("ok");
     }
     else
     {
         return Content("error");
     }
 } 
 /// <summary>
 /// 下载
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public FileResult Download(int id)
 {
     Resource resource = new Resource();
     resource = db.Resources.Find(id);
     var path = Server.MapPath("~/Upload/" + resource.Path);
     return File(path, "1", Url.Encode(resource.Path));
 }