public ActionResult Create([Bind(Include = "TicketId")] TicketAttachment ticketAttachment, string attachmentTitle, string attachmentDescription, HttpPostedFileBase attachment) { if (ModelState.IsValid) { ticketAttachment.Title = attachmentTitle; ticketAttachment.Description = attachmentDescription; ticketAttachment.Created = DateTime.Now; ticketAttachment.UserId = User.Identity.GetUserId(); //This is where i will use my attachment upload validator to decide whether this is a good file or not if (ImageUploader.IsValidAttachment(attachment)) { var fileName = Path.GetFileName(attachment.FileName); attachment.SaveAs(Path.Combine(Server.MapPath("~/Attachments/"), fileName)); ticketAttachment.AttachmentUrl = "/Attachments/" + fileName; } db.TicketAttachments.Add(ticketAttachment); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.TicketId = new SelectList(db.Tickets, "Id", "OwnerUserId", ticketAttachment.TicketId); ViewBag.UserId = new SelectList(db.Users, "Id", "FirstName", ticketAttachment.UserId); return(View(ticketAttachment)); }
public ActionResult Create([Bind(Include = "TicketId")] TicketAttachment ticketAttachment, HttpPostedFileBase FilePath, string attachmentDescription) { ticketAttachment.Description = attachmentDescription; ticketAttachment.Created = DateTime.Now; ticketAttachment.UserId = User.Identity.GetUserId(); if (ModelState.IsValid) { //Use Attachment Upload Validator to verify file if (ImageUploader.IsValidAttachment(FilePath)) { var fileName = Path.GetFileName(FilePath.FileName); var ext = Path.GetExtension(FilePath.FileName); var unique = $"{fileName}-{DateTime.Now}"; var slug = SlugHelper.CreateSlug(unique); var formattedFile = $"{slug}{ext}"; FilePath.SaveAs(Path.Combine(Server.MapPath("~/Attachments/"), formattedFile)); ticketAttachment.FilePath = "/Attachments/" + formattedFile; } db.TicketAttachments.Add(ticketAttachment); db.SaveChanges(); return(RedirectToAction("Dashboard", "Home")); } ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketAttachment.TicketId); ViewBag.UserId = new SelectList(db.Users, "Id", "FirstName", ticketAttachment.UserId); return(View(ticketAttachment)); }