Skip to content

The upload stream is closing after the loop. #33272

@ghost

Description

[EDIT by guardrex to fix the code block ... @rodrigo-lemela-duarte, it's triple-backticks on a line above and below code in a comment]
[EDIT by guardrex to update the report on the var access that results in the error about the closed stream. The error occurs on content access.]

Description

At the FileUpload2.razor code:

This part of the code is throwning error:

var fileContent = new StreamContent(file.OpenReadStream(maxFileSize));

After you get out of the loop and try to access fileContent [should be content, see comment below], .NET throws an error saying that the stream is closed.

The right piece of the code is:

await using var fileStream = uploadedFile.OpenReadStream(maxAllowedSize: maxFileSize);
using var memoryStream = new MemoryStream();
await fileStream.CopyToAsync(memoryStream);
var fileContent = new ByteArrayContent(memoryStream.ToArray());

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/file-uploads.md

Document ID

c11d981c-05af-c19d-a333-feedd5978639

Article author

@guardrex

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions