public JsonResult DeleteAttachment(string instanceId, string type, string id) { string msg = string.Empty; bool state = false; Common.Entity.AttachmentEntity file = Common.Entity.AttachmentEntity.GetSingle(id); if (file != null && System.IO.File.Exists(file.FileAddress)) { System.IO.File.Delete(file.FileAddress); file.Delete(out msg); //删除 state = true; } else { msg = "文件不存在,请联系管理员!"; state = false; } return(new JsonResult { Data = new { state = state, msg = msg }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }); }
// // GET: /Common/ public JsonResult UploadAttachment() { bool state = true; string msg = string.Empty; List <Common.Entity.AttachmentEntity> fileList = new List <Common.Entity.AttachmentEntity>(); try { HttpFileCollectionBase files = Request.Files; if (files.Count == 0) { state = false; msg = "没有附件"; } else { string dirPath = Common.Util.Util.CreateDirectory(AttachmentPath, true); for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; Common.Entity.AttachmentEntity AttachmentEntity = new Common.Entity.AttachmentEntity { FileName = Path.GetFileName(file.FileName), FileType = Path.GetExtension(file.FileName), FileAddress = dirPath + Guid.NewGuid().ToString() + Path.GetExtension(file.FileName), Creator = HttpContext.User.Identity.Name, CreateTime = DateTime.Now }; file.SaveAs(AttachmentEntity.FileAddress); FileInfo fileInfo = new FileInfo(AttachmentEntity.FileAddress); AttachmentEntity.FileSize = fileInfo.Length; state = AttachmentEntity.Save(out msg); //出错则终止上传 if (!state) { fileInfo.Delete(); break; } fileList.Add(AttachmentEntity); } } } catch (Exception e) { state = false; msg = e.Message; } return(new JsonResult { Data = new { state = state, msg = msg, data = fileList }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }); }
// Get: 附件下载 public void DownloadAttachment(int id) { string msg = string.Empty; Common.Entity.AttachmentEntity file = Common.Entity.AttachmentEntity.GetSingle(id); if (file != null && System.IO.File.Exists(file.FileAddress)) { Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.FileName)); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FileAddress); } else { Response.Write("文件不存在,请联系管理员!"); Response.End(); } }