aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

.NET 10, ReadNextSectionAsync has bug,when postman submit single file throw System.IO.InvalidDataException:“The stream exceeded the data limit 16384.”, when postman submit 1+ files, ReadNextSectionAsync never return null。

Open justmine2514 opened this issue 1 month ago • 3 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the bug

.NET 10, ReadNextSectionAsync has bug,when postman submit single file throw System.IO.InvalidDataException:“The stream exceeded the data limit 16384.”, when postman submit 1+ files, ReadNextSectionAsync never return null。

protected override async Task<IReadOnlyList<WebBlob>?> HandleAsync(
    HttpRequest request,
    CancellationToken token = default)
{
    if (!MediaTypeHeaderValue.TryParse(request.ContentType, out var mediaTypeHeader) ||
        string.IsNullOrWhiteSpace(mediaTypeHeader.Boundary.Value))
        throw HttpApiExceptions.BadRequest($"`Content-Type` is form-data, but `boundary` not found");
    var boundary = HeaderUtilities.RemoveQuotes(mediaTypeHeader.Boundary).Value ?? throw HttpApiExceptions.BadRequest($"`Content-Type` is form-data, but `boundary` not found");
    var reader = new MultipartReader(boundary, request.Body);
    var section = await reader.ReadNextSectionAsync(token);
    var hasWebBlob = false;
    var storageOptions = BlobStorageService.Options;
    var webBlobs = new Lazy<List<WebBlob>>(() => new List<WebBlob>(storageOptions.MaxFilesPerTime));
    while (section != null)
    {
        var disposition = section.GetContentDispositionHeader();
        if (disposition != null &&
            disposition.IsFileDisposition() &&
            !string.IsNullOrWhiteSpace(disposition.FileName.Value))
        {
            var fileSection = section.AsFileSection();
            if (fileSection?.FileStream is null) continue;
            var fileType = Path.GetExtension(disposition.FileName).ToString();
            if (!storageOptions.IsFileTypePermitted(fileType))
                throw HttpApiExceptions.BadRequest($"Illegal file type:{fileType}");
            // handle file upload 
        }
        // .NET 10, ReadNextSectionAsync has bug,when postman submit single file throw System.IO.InvalidDataException:“The stream exceeded the data limit 16384.”, when postman submit 1+ files, ReadNextSectionAsync never return null。
        section = await reader.ReadNextSectionAsync(token);
    }
    return hasWebBlob ? webBlobs.Value : null;
}

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

justmine2514 avatar Dec 08 '25 11:12 justmine2514

I'm a bot. Here are possible related and/or duplicate issues (I may be wrong):

  • https://github.com/dotnet/aspnetcore/issues/58233
  • https://github.com/dotnet/aspnetcore/issues/50252

MihuBot avatar Dec 08 '25 11:12 MihuBot

Is it possible to provide the raw HTTP request that postman sends for both these cases? Per company policy, we're not allowed to run postman. And from the linked issues, it seems like postman might be sending the form in an "unusual" way that we aren't familiar with since "normal" file uploads work when we try them.

BrennanConroy avatar Dec 09 '25 19:12 BrennanConroy

use scalar test

Image

justmine2514 avatar Dec 10 '25 02:12 justmine2514