I am trying to implement a file upload component in my Blazor web application. I am using the approach provided on the Core Blazor InputFile component documentation page.
Here is my code:
await using FileStream fs = new(path, FileMode.Create);
await file.OpenReadStream(MaxFileSize).CopyToAsync(fs);
If OpenReadStream throws an error, (MaxFileSize is exceeded, etc.) the file should not be uploaded. However, since I already created the file stream, an empty file is created at the provided path (an empty text document for example). I am a C# and Blazor newbie so I'm probably just overlooking an easy solution. How can I ensure that an empty file is not uploaded even if an error is thrown?