Skip to content

Commit

Permalink
FileSaver does not truncate existing files (#1050)
Browse files Browse the repository at this point in the history
Fix #1049: FileSaver does not truncate existing files

FileMode.OpenOrCreate does not automatically truncate files.

This fix truncates files to ensure that junk data does not appear at the
end of a newly saved file if the data being written is shorter than data
present in a previously existing file.
  • Loading branch information
Greg Dietsche authored Mar 2, 2023
1 parent 4e255ef commit 30c86b7
Showing 1 changed file with 1 addition and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async Task<FileSaverResult> SaveAsync(string fileName, Stream stream, Can
static async Task WriteStream(Stream stream, string filePath, CancellationToken cancellationToken)
{
await using var fileStream = new FileStream(filePath, FileMode.OpenOrCreate);
fileStream.SetLength(0);
stream.Seek(0, SeekOrigin.Begin);
await stream.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
}
Expand Down

0 comments on commit 30c86b7

Please sign in to comment.