Cannot upload more than 4 files to the api server from blazor side

Viewed 31

I'm trying to add a new function where user can upload images and videos similar to facebook in blazor server .NET 6, I created a new api and followed microsoft documentation of uploading files, Everything is working so far with uploading up to 4 files, but when I'm trying to upload more than 4 it doesn't send a request to the api at all. and after a while it shows these two exceptionsenter image description here enter image description here Here's the code I'm having: `

private List<File> files = new();
private List<UploadInfo> uploadResults = new();
private int maxAllowedFiles = 10;
private bool shouldRender;

protected override bool ShouldRender() => shouldRender;

private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
    shouldRender = false;
    long maxFileSize = (long)(4 * Math.Pow(10, 8));
    var upload = false;

    using var content = new MultipartFormDataContent();

    foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
    {
        if (uploadResults.SingleOrDefault(
            f => f.FileName == file.Name) is null)
        {
            try
            {
                var fileContent = 
                    new StreamContent(file.OpenReadStream(maxFileSize));

                fileContent.Headers.ContentType = 
                    new MediaTypeHeaderValue(file.ContentType);

                files.Add(new() { Name = file.Name });

                content.Add(
                    content: fileContent,
                    name: "\"files\"",
                    fileName: file.Name);

                upload = true;
            }
            catch (Exception ex)
            {
                Logger.LogInformation(
                    "{FileName} not uploaded (Err: 6): {Message}", 
                    file.Name, ex.Message);

                uploadResults.Add(
                    new()
                    {
                        FileName = file.Name, 
                        ErrorCode = 6, 
                        Uploaded = false
                    });
            }
        }
    }

    if (upload)
    {
        var client = ClientFactory.CreateClient();
        var response = 
            await client.PostAsync($"https://localhost:7134/api/Files/FileUpload", content);

        if (response.IsSuccessStatusCode)
        {
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
            };

            using var responseStream =
                await response.Content.ReadAsStreamAsync();

            var newUploadResults = await JsonSerializer
                .DeserializeAsync<IList<MediaAPI.UploadInfo>>(responseStream, options);

            if (newUploadResults is not null)
            {
                uploadResults = uploadResults.Concat(newUploadResults).ToList();
            }
        }
    }

    shouldRender = true;
}`

When I try to debug it, the debug point disappears when it sends the request: await client.PostAsync($"https://localhost:7134/api/Files/FileUpload", content); Anyone has experiansed this issue before? Thanks in advance.

0 Answers
Related