Skip to content

Commit

Permalink
Fix #1049: FileSaver does not truncate existing files
Browse files Browse the repository at this point in the history
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
farmergreg authored and Greg Dietsche committed Mar 1, 2023
1 parent 4e255ef commit 99775fd
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 99775fd

Please sign in to comment.