Azure Function Form data file upload is NullStream

I have a setup of Blazor server application as frontend and a Azure function app as backend.
The user will be uploading a file to the blazor server which directly forwards the file the azure function API.

My problem is that I am not able to forward the provided stream to the HttpClient, which is calling the Azure function app. On the azure function side I receive as “body” as NullStream which does not have data.

I have the following system architecture. The whole code base is in dotnet 8.

Blazor Server application

The user can upload files to the blazor server application. This is working as expected. The formfiles are correctly taken and the stream can be correctly deserialized.

Blazor UI Component


@inject IApiClient ApiClient;


<div style="width: 100%; display: flex; align-items: center; justify-content: center;">

    <label for="inputFile" class="fileUpload">
        <InputFile id="inputFile" OnChange="@LoadFiles" class="d-none" />
    </label>
</div>



@code {

    private long _maxFileSize = 10 * 1024 * 1024;
    private int _maxAllowedFiles = 1;

    private async Task LoadFiles(InputFileChangeEventArgs e)
    {

        foreach (var file in e.GetMultipleFiles(_maxAllowedFiles))
        {
            try
            {
                var fileStream = file.OpenReadStream(_maxFileSize);
                var fileMetaData = await ApiClient.UploadFileAsync(fileStream, file.ContentType, file.Name);
                // omitted
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Service function which is calling the API backend
I don’t want to load the whole stream into memory but rather directly provide it as streamcontent to the multipart formdata.

public async Task<FileMetaDataDto> UploadFileAsync(Stream data, string contentType, string fileName)
{
    var content = new MultipartFormDataContent();
    content.Add(new StreamContent(data), fileName);

        // this would copy the whole stream into memory
    // await content.ReadAsStringAsync();

    var result = await _httpClient.PostAsync("api/v1/files", content);

    var json = await result.Content.ReadAsStringAsync();

    var deserialized = JsonSerializer.Deserialize<FileMetaDataDto>(json, SerializerExtensions.DefaultOptions());

    return deserialized ?? new FileMetaDataDto();
}

Azure function Httptrigger

The upload function is a HttpTrigger. The function itself is isolated model version 4.0.
I use as an external dependency the following package:

<PackageReference Include=”HttpMultipartParser” Version=”5.0.0″ />

[Function("UploadFile")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/files")] HttpRequestData req)
{
    try
    {
        var body = req.Body;
        var parsedFormBody = MultipartFormDataParser.ParseAsync(req.Body);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    return req.CreateResponse(HttpStatusCode.OK);
}

Testing with Postman / Swagger

When using the API directly with postman or Swagger the Azure function API behaves correctly. But I also noticed that both are loading files first into memory and sending the whole data at once.
In this case the req.Body is of type MemoryStream.

Loading data in memory in Blazor server

The same works if I upload the whole user provided file firs into memory (e.g. copying to a MemoryStream or calling “await content.ReadAsStringAsync();”

Related questions

Using Azure Function .NET5 and HttpRequestData, how to handle file upload (form data)?
My starting point was this question, but it did not solve the problem.

Azure functions – How to read form data
Outdated

.net 5 Azure Function multipart file upload issue
Uploads the file completely in memory in the frontend.

Next steps

I am uncertain where the error is located

1- Blazor server.
Do I use correctly the Httpclient together with Multipart form data?
Again I do not want to load the whole stream into memory within the blazor server app.

2- Azure function
Is this the correct way to get form data?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật