public async Task <IActionResult> Edit(Guid id, [Bind("Id,ForumMessageId,Created,FileName,FilePath")] ForumMessageAttachment forumMessageAttachment) { if (id != forumMessageAttachment.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(forumMessageAttachment); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ForumMessageAttachmentExists(forumMessageAttachment.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ForumMessageId"] = new SelectList(_context.ForumMessages, "Id", "ApplicationUserId", forumMessageAttachment.ForumMessageId); return(View(forumMessageAttachment)); }
public async Task <IActionResult> Create(Guid?messageId, ForumMessageAttachmentCreateModel model) { if (messageId == null) { return(NotFound()); } var message = await _context.ForumMessages .Include(m => m.ForumTopic) .SingleOrDefaultAsync(x => x.Id == messageId); if (message == null || !this.userPermissions.CanEditTopicMessage(message)) { return(NotFound()); } var fileName = Path.GetFileName(ContentDispositionHeaderValue.Parse(model.File.ContentDisposition).FileName.Trim('"')); var fileExt = Path.GetExtension(fileName); this.ViewBag.Message = message; if (!AllowedExtensions.Contains(fileExt)) { this.ModelState.AddModelError(nameof(model.File), "This file type is prohibited"); } if (ModelState.IsValid) { var attachment = new ForumMessageAttachment() { Id = Guid.NewGuid(), ForumMessageId = message.Id, Created = DateTime.UtcNow, FileName = fileName }; var path = Path.Combine(this.hostingEnvironment.WebRootPath, "attachments", attachment.Id.ToString("N") + fileExt); attachment.FilePath = $"/attachments/{attachment.Id:N}{fileExt}"; using (var fileStream = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read)) { await model.File.CopyToAsync(fileStream); } _context.Add(attachment); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "ForumTopics", new { id = message.ForumTopicId, forumId = message.ForumTopic.ForumId })); } return(View(model)); }
public async Task <IActionResult> Create(Guid?messageId, ForumMessageAttachmentViewModel model) { if (messageId == null) { return(this.NotFound()); } var message = await this.context.ForumMessages .SingleOrDefaultAsync(m => m.Id == messageId); if (message == null) { return(this.NotFound()); } var fileName = Path.GetFileName(ContentDispositionHeaderValue.Parse(model.File.ContentDisposition).FileName.Trim('"')); var fileExt = Path.GetExtension(fileName); if (!AllowedExtensions.Contains(fileExt)) { this.ModelState.AddModelError(nameof(model.File), "This file type is prohibited"); } if (this.ModelState.IsValid) { var forumMessageAttachment = new ForumMessageAttachment { MessageId = message.Id, Created = DateTime.UtcNow, FileName = fileName }; var attachmentPath = Path.Combine(this.hostingEnvironment.WebRootPath, "attachments", forumMessageAttachment.Id.ToString("N") + fileExt); forumMessageAttachment.FilePath = $"/attachments/{forumMessageAttachment.Id:N}{fileExt}"; using (var fileStream = new FileStream(attachmentPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read)) { await model.File.CopyToAsync(fileStream); } this.context.Add(forumMessageAttachment); await this.context.SaveChangesAsync(); return(this.RedirectToAction("Index", "ForumMessages", new { topicId = message.TopicId })); } this.ViewBag.Message = message; return(this.View(model)); }
public async Task <IActionResult> Create(Guid?Id, ForumAttachmentCreateModel model) { if (Id == null) { return(this.NotFound()); } var message = await this._context.ForumMessages .Include(w => w.ForumMessageAttachments) .SingleOrDefaultAsync(x => x.Id == Id); if (message == null) { return(this.NotFound()); } var fileName = Path.GetFileName(ContentDispositionHeaderValue.Parse(model.FilePath.ContentDisposition).FileName.Trim('"')); var fileExt = Path.GetExtension(fileName); if (this.ModelState.IsValid) { var attachment = new ForumMessageAttachment { Created = DateTime.Now, }; var path = Path.Combine(_hostingEnvironment.WebRootPath, "attachments", attachment.Id.ToString("N") + fileExt); attachment.FilePath = $"/attachments/{attachment.Id:N}{fileExt}"; attachment.FileName = fileName; using (var fileStream = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read)) { await model.FilePath.CopyToAsync(fileStream); } message.ForumMessageAttachments.Add(attachment); this._context.Add(attachment); await this._context.SaveChangesAsync(); return(this.RedirectToAction("Index", new { forumMessageId = message.Id })); } this.ViewBag.Hospital = message; return(this.View(model)); }