Skip to content

Multipart body length limit 16384 exceeded #15098

@myjimmyzhou

Description

@myjimmyzhou

I encountered errors in the following situations:
code like this:

public UploadFileDto UploadFile(HttpRequest request)
        {
            var trustedFileNameForDisplay = string.Empty;
            //var untrustedFileNameForStorage = string.Empty;
            var streamedFileContent = new byte[0];
            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType), _formOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, request.Body);
            var section = reader.ReadNextSectionAsync().Result;
            while (section != null)
            {
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                //untrustedFileNameForStorage = contentDisposition.FileName.Value;
                trustedFileNameForDisplay = WebUtility.HtmlEncode(contentDisposition.FileName.Value);
                var ext = Path.GetExtension(trustedFileNameForDisplay).ToLowerInvariant();
                if (string.IsNullOrEmpty(ext) || !_permittedExtensions.Contains(ext))
                {
                    throw new Exception("file is error");
                }

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        streamedFileContent = FileHelpers.ProcessStreamedFile(section, _fileSizeLimit);
                    }
                }
                section = reader.ReadNextSectionAsync().Result;
            }
            return new UploadFileDto
            {
                FileContent = streamedFileContent,
                FileName = trustedFileNameForDisplay
            };
        }

1: this exception to be thrown when I use http and file size > 2MB
2: It works like this:
http,file size < 2MB ;
https ,no file size limit .

Another situation
use async code ,It works too,
code like this:

 public async Task<UploadFileDto> UploadFile(HttpRequest request)
        {
            var trustedFileNameForDisplay = string.Empty;
            //var untrustedFileNameForStorage = string.Empty;
            var streamedFileContent = new byte[0];
            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType), _formOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, request.Body);
            var section = await reader.ReadNextSectionAsync();
            while (section != null)
            {
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                //untrustedFileNameForStorage = contentDisposition.FileName.Value;
                trustedFileNameForDisplay = WebUtility.HtmlEncode(contentDisposition.FileName.Value);
                var ext = Path.GetExtension(trustedFileNameForDisplay).ToLowerInvariant();
                if (string.IsNullOrEmpty(ext) || !_permittedExtensions.Contains(ext))
                {
                    throw new Exception("file is error");
                }

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        streamedFileContent = FileHelpers.ProcessStreamedFile(section, _fileSizeLimit);
                    }
                }
                section = await reader.ReadNextSectionAsync();
            }
            return new UploadFileDto
            {
                FileContent = streamedFileContent,
                FileName = trustedFileNameForDisplay
            };
        }

I don't know why there's an exception when I use http and file size > 2MB.
but other situation is working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions