Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The upload stream is closing after the loop. #33272

Closed
rodrigo-lemela-duarte opened this issue Aug 6, 2024 · 8 comments · Fixed by #33357
Closed

The upload stream is closing after the loop. #33272

rodrigo-lemela-duarte opened this issue Aug 6, 2024 · 8 comments · Fixed by #33357

Comments

@rodrigo-lemela-duarte
Copy link

rodrigo-lemela-duarte commented Aug 6, 2024

[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

Copy link
Contributor

github-actions bot commented Aug 6, 2024

😎🏖️🌴 Summertime! 🏐🏄‍♀️🦩

Stand by! A green dinosaur 🦖 (guardrex) will arrive shortly to assist.

@guardrex
Copy link
Collaborator

guardrex commented Aug 6, 2024

Thanks @rodrigo-lemela-duarte for opening the issue over here. This is the repo where we work issues.

That code, which is part of the FileUpload2 component, isn't supposed to be used that way. The contents of fileContent are only used to establish content for the MultipartFormDataContent, which is used to POST the file content to the server. This scenario is described at the top of the section ...

The following example demonstrates uploading files from a server-side app to a backend web API controller in a separate app, possibly on a separate server.

I think the scenario that you should focus on for a BWA uploading content to the BWA is in the Server-side file upload example section ...

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

@rodrigo-lemela-duarte
Copy link
Author

rodrigo-lemela-duarte commented Aug 6, 2024

Hey,

I express my self wrongly and I apoligizy for.
The correct information is:

When you declare that way and use the content (using var content = new MultipartFormDataContent();) on the PostAsync, get the error about the closed stream.
That is the correct statement and the correction is that, in that piece of code.

Why this happen? I don't know, I didn't got deepper on the issue, just look for the correct code and move on.

Thanks.

@guardrex
Copy link
Collaborator

guardrex commented Aug 6, 2024

I see. Thanks for the clarification.

Let me see if I can reproduce that error here. I'll get back to you soon ... hopefully by tomorrow afternoon.

@guardrex
Copy link
Collaborator

guardrex commented Aug 7, 2024

I'll leave this open for one of the product unit engineers to take a look during the holidays (or sooner if more bug reports come in), but I can't reproduce a problem with the current demonstration code ...

Capture

After changing the generated file extensions to their correct values (.png and .txt) ...

Capture1 Capture2

@rodrigo-lemela-duarte
Copy link
Author

Strange. Can be something local?
After that change, mentioned on the first comment, my code started to work.

@guardrex
Copy link
Collaborator

guardrex commented Aug 8, 2024

I don't know ... maybe. The code in the article was approved by the product unit. We'll have to have one of the product unit engineers take a look at this issue.

However, they can't look right now. They're too busy putting the final touches on the .NET 9 release for November.

I'll contact them around the holidays in late November to take a look.

@guardrex
Copy link
Collaborator

@rodrigo-lemela-duarte ... Mackinnon just remarked on this (and similar problems). I'll work on adding a section and/or updating the existing approach. See ....

dotnet/aspnetcore#38842

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants