public ActionResult EditAttachment(int?id, int?attachmentId) { var model = new CreateEditAttachmentViewModel(); var attachment = bugTrackerHelper.GetAttachmentById(attachmentId.Value); model = Mapper.Map <CreateEditAttachmentViewModel>(attachment); return(View(model)); }
private ActionResult SaveAttachment(int?id, int?attachmentId, CreateEditAttachmentViewModel formData) { if (formData.Media == null && attachmentId == null) { ModelState.AddModelError("FileURL", "Please upload file"); return(View()); } if (!ModelState.IsValid || !id.HasValue) { return(RedirectToAction("AllTickets", "Ticket")); } TicketAttachment attachment = new TicketAttachment(); var ticket = bugTrackerHelper.GetCurrentTicketById(id.Value); var message = notificationHelper.CreateAttachmentNotification(ticket.Title); if (attachmentId == null) { attachment.DateCreated = DateTime.Now; attachment.TicketId = id.Value; attachment.UserId = User.Identity.GetUserId(); DbContext.TicketAttachments.Add(attachment); notificationHelper.SendNotification(ticket, message, false); } else { attachment = bugTrackerHelper.GetAttachmentById(attachmentId.Value); } attachment.Description = formData.Description; if (formData.Media.ContentLength > 2029152) { ModelState.AddModelError("IsMaxRequestExceededException", "The file's size cannot exсeed 2Mb"); return(View()); } FileUpload(attachment, formData); DbContext.SaveChanges(); return(RedirectToAction("TicketDetails", "Ticket", new { id = attachment.TicketId })); }
private void FileUpload(TicketAttachment attachment, CreateEditAttachmentViewModel formData) { string fileExtension; if (formData.Media != null) { fileExtension = Path.GetExtension(formData.Media.FileName).ToLower(); //Create directory if it doesn't exists if (!Directory.Exists(AttachmentConstants.MappedUploadFolder)) { Directory.CreateDirectory(AttachmentConstants.MappedUploadFolder); } //Get file name with Guid to prevent name repetition //and calculate full path with upload folder which is in constants var fileName = Guid.NewGuid().ToString() + fileExtension; attachment.FileName = fileName; var fullPathWithName = AttachmentConstants.MappedUploadFolder + fileName; //Actual save on hard disk formData.Media.SaveAs(fullPathWithName); attachment.FilePath = fullPathWithName; attachment.FileUrl = AttachmentConstants.UploadFolder + fileName; } }
public ActionResult EditAttachment(int?id, int?attachmentId, CreateEditAttachmentViewModel formData) { return(SaveAttachment(id, attachmentId, formData)); }
public ActionResult CreateAttachment(int?id, CreateEditAttachmentViewModel formData) { return(SaveAttachment(id, null, formData)); }