/// <summary> /// Returns the partial view for Uploading Attachments /// </summary> /// <param name="Id">Sting Id of Model for attachment to be created</param> /// <returns></returns> public PartialViewResult UploadAttachmentView(string Id) { CreateAttachmentViewModel model = new CreateAttachmentViewModel(Id); model.TimelineEventId = Id; return(PartialView("_UploadAttachment", model)); }
public ActionResult Create(CreateAttachmentViewModel model, HttpPostedFileBase attachment) { if (attachment.ContentLength > 0 && ModelState.IsValid) { var fileName = UploadFile(attachment); model.FileName = fileName; _attachmentsService.Create(model); return(new RedirectResult(Url.Action("Manage", "Projects", new { id = model.Id }) + "#attachments")); } return(View(model)); }
public void Create(CreateAttachmentViewModel model) { var attachment = new Attachment { Title = model.Title, Description = model.Description, ProjectId = model.Id, UploadDate = GlobalSettings.CURRENT_DATETIME, FileName = model.FileName }; attachment.Size = (int)new FileInfo(_uploadPath + model.FileName).Length; _unitOfWork.Attachments.Add(attachment); _unitOfWork.SaveChanges(); }
public async Task <IActionResult> CreateAttachment([FromBody] CreateAttachmentViewModel model, [FromRoute] int issueId) { int size = model.Content.Length; IssueAttachment attachment = new IssueAttachment { IssueId = issueId, Content = model.Content, Comment = model.Comment }; Database.IssueAttachment.Add(attachment); int userId = int.Parse(User.Claims.First(c => c.Type == "UserId").Value); var user = Database.User .Where(u => u.Id == userId) .FirstOrDefault(); var issue = Database.Issue .Include(i => i.ProjectVersion) .Where(i => i.Id == issueId) .FirstOrDefault(); await Database.SaveChangesAsync(); ProjectActivity activity = new ProjectActivity { ProjectId = issue.ProjectVersion.ProjectId, AuthorId = userId, Content = $"File attached to issue '#{issue.Id}: {issue.Subject}'" }; Database.ProjectActivity.Add(activity); await Database.SaveChangesAsync(); return(Json(new { status = true, url = Url.Action("Index") })); }
/// <summary> /// Uploads an attachment to an event /// </summary> /// <param name="model">CreateAttachmentViewModel of the data of the attachment and event</param> /// <returns>Redirect to TimelineView</returns> public async Task <ActionResult> UploadAttachment(CreateAttachmentViewModel model) { //Sets Title and URL using fileName and getting presigned URL var title = model.File.FileName; var url = ""; try //Error Handling { url = GenerateUploadPreSignedUrl(title); } catch { return(RedirectToAction("APIError")); } //Sets up webRequest variable var httpRequest = WebRequest.Create(url) as HttpWebRequest; httpRequest.Method = "PUT"; var filePath = Path.GetTempFileName(); //Copies file to tempoary folder using (var stream = new FileStream(filePath, FileMode.Create)) { await model.File.CopyToAsync(stream); } //Writes the file to the Datastream using (var dataStream = httpRequest.GetRequestStream()) { var buffer = new byte[18000]; using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { dataStream.Write(buffer, 0, bytesRead); } } } //Creates the attachment try //Error Handling { SendCreateAttachmentRequest(title, model.TimelineEventId); } catch { return(RedirectToAction("APIError")); } var response = httpRequest.GetResponse() as HttpWebResponse; if (!response.StatusDescription.Equals("OK")) { return(RedirectToAction("APIError")); } _toastNotification.AddSuccessToastMessage("Attachment has uploaded!"); return(RedirectToAction("TimelineView", "Event", new { id = GetTimelineID(model.TimelineEventId) })); //returns to the Index! }