Exemplo n.º 1
0
        protected async Task <Guid?> UploadFile()
        {
            var authorId = Guid.Parse(HttpContext.User.Identity.Name);

            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(null);
            }

            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), Constants.MultipartBoundaryLengthLimit);
            var reader   = new MultipartReader(boundary, HttpContext.Request.Body);
            var section  = await reader.ReadNextSectionAsync().ConfigureAwait(true);

            while (section != null)
            {
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        var nameHtmlEncoded = WebUtility.HtmlEncode(contentDisposition.FileName.Value);

                        return(await fileRepo.SaveAttachment(section, nameHtmlEncoded, authorId).ConfigureAwait(true));
                    }
                }

                section = await reader.ReadNextSectionAsync();
            }

            return(null);
        }