예제 #1
0
        public async Task <List <AttachmentDTO> > Add(int solutionId, HttpRequestMessage request)
        {
            try
            {
                // Check if the request contains multipart/form-data.
                if (!request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                using (UoW)
                {
                    var solution = UoW.Solutions.Get(solutionId);
                    if (solution == null)
                    {
                        throw new ApplicationOperationException(string.Format("Solution with solutionId {0} not found", solutionId), HttpStatusCode.NotFound);
                    }

                    var provider = new StreamProvider(root);
                    await request.Content.ReadAsMultipartAsync(provider);

                    var attachments = new List <SolutionAttachment>();
                    foreach (MultipartFileData file in provider.FileData)
                    {
                        var attachment = new SolutionAttachment()
                        {
                            Author           = CurrentUser,
                            Solution         = solution,
                            UploadDate       = DateTime.UtcNow,
                            OriginalFileName = file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty),
                            FileName         = file.LocalFileName.Substring(file.LocalFileName.LastIndexOf('\\') + 1)
                        };
                        attachments.Add(attachment);
                    }
                    UoW.SolutionAttachments.AddRange(attachments);
                    UoW.Complete();
                    return(Mapper.Map <List <AttachmentDTO> >(attachments));
                }
            }
            catch (System.Exception e)
            {
                throw new ApplicationOperationException(e.Message, HttpStatusCode.InternalServerError);
            }
        }
예제 #2
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var solution = Mapper.Map <Solution>(Model);

            solution.AuthorId = this.userManager.GetUserId(User);


            await this.solutionService.AddAsync(solution);

            if (Model.Attachments != null)
            {
                string path = await fileUploader.CreateAttachmentAsync(Model.Title, Model.Attachments, "Solutions");

                ICollection <SolutionAttachment> attachments = new List <SolutionAttachment>();

                foreach (var attachment in Model.Attachments)
                {
                    SolutionAttachment solutionAttachment = new SolutionAttachment
                    {
                        FileName   = attachment.FileName,
                        PathToFile = Path.Combine(path, attachment.FileName),
                        SolutionId = solution.Id
                    };
                    attachments.Add(solutionAttachment);
                }

                await this.attachmentService.AddRangeAsync(attachments);
            }

            await this.solutionService.SaveChangesAsync();

            alerter.AddMessage(MessageType.Success, "Solution created successfully");

            return(Redirect($"/Solutions/Details/{solution.Id}"));
        }