public virtual ActionResult AttachmentUpload(string id, string Domain, string Comments) { id = ActiveDirectory.ParseDomainAccountId(id, Domain); var u = Database.Users.Find(id); if (u != null) { if (Request.Files.Count > 0) { var file = Request.Files.Get(0); if (file.ContentLength > 0) { var contentType = file.ContentType; if (string.IsNullOrEmpty(contentType) || contentType.Equals("unknown/unknown", StringComparison.OrdinalIgnoreCase)) contentType = MimeTypes.ResolveMimeType(file.FileName); var ua = new Disco.Models.Repository.UserAttachment() { UserId = u.UserId, TechUserId = CurrentUser.UserId, Filename = file.FileName, MimeType = contentType, Timestamp = DateTime.Now, Comments = Comments }; Database.UserAttachments.Add(ua); Database.SaveChanges(); ua.SaveAttachment(Database, file.InputStream); ua.GenerateThumbnail(Database); return Json(ua.Id, JsonRequestBehavior.AllowGet); } } throw new Exception("No Attachment Uploaded"); } throw new Exception("Invalid User Id"); }
public static UserAttachment CreateAttachment(this User User, DiscoDataContext Database, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, Image PdfThumbnail = null) { if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.OrdinalIgnoreCase)) MimeType = Interop.MimeTypes.ResolveMimeType(Filename); UserAttachment ua = new UserAttachment() { UserId = User.UserId, TechUserId = CreatorUser.UserId, Filename = Filename, MimeType = MimeType, Timestamp = DateTime.Now, Comments = Comments }; if (DocumentTemplate != null) ua.DocumentTemplateId = DocumentTemplate.Id; Database.UserAttachments.Add(ua); Database.SaveChanges(); ua.SaveAttachment(Database, Content); Content.Position = 0; if (PdfThumbnail == null) ua.GenerateThumbnail(Database, Content); else ua.SaveThumbnailAttachment(Database, PdfThumbnail); return ua; }