Esempio n. 1
0
        /// <summary>
        /// Creates a new instance of the <see cref="FileMultipartSection"/> class
        /// </summary>
        /// <param name="section">The section from which to create the <see cref="FileMultipartSection"/></param>
        /// <param name="header">An already parsed content disposition header</param>
        public FileMultipartSection(MultipartSection section, ContentDispositionHeaderValue header)
        {
            if (!header.IsFileDisposition())
            {
                throw new ArgumentException($"Argument must be a file section", nameof(section));
            }
            Section = section;
            _contentDispositionHeader = header;

            Name     = HeaderUtilities.RemoveQuotes(_contentDispositionHeader.Name).ToString();
            FileName = HeaderUtilities.RemoveQuotes(
                _contentDispositionHeader.FileNameStar.HasValue ?
                _contentDispositionHeader.FileNameStar :
                _contentDispositionHeader.FileName).ToString();
        }
Esempio n. 2
0
        public async Task <IActionResult> Upload()
        {
            MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(Request.ContentType);

            string boundary = HeaderUtilities.RemoveQuotes(mediaTypeHeaderValue.Boundary).ToString();

            MultipartReader reader = new MultipartReader(boundary, Request.Body, 64 * 1024);

            MultipartSection section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                ContentDispositionHeaderValue contentDispositionHeaderValue = section.GetContentDispositionHeader();

                if (!contentDispositionHeaderValue.IsFileDisposition())
                {
                    throw new InvalidOperationException("Only file disposition supported.");
                }

                FileMultipartSection fileSection = section.AsFileSection();

                int bufferSize = 64 * 1024;

                string fileName = Path.Combine(_targetFilePath, Path.GetFileName(fileSection.FileName));

                using (FileStream targetStream = System.IO.File.Create(fileName, bufferSize)) {
                    await fileSection.FileStream.CopyToAsync(targetStream);

                    _logger.LogInformation($"File uploaded to: {fileName}");
                }

                section = await reader.ReadNextSectionAsync();
            }

            return(Ok());
        }