From 29d5ad3a1fde229b9dca0c82a3840744350ab5c1 Mon Sep 17 00:00:00 2001 From: David West Date: Thu, 23 May 2024 15:16:20 -0700 Subject: [PATCH 1/2] Reset stream position before posting to server --- clients/dotnet/WebClient/MemoryWebClient.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/clients/dotnet/WebClient/MemoryWebClient.cs b/clients/dotnet/WebClient/MemoryWebClient.cs index 281f9b5cf..8f64a6d95 100644 --- a/clients/dotnet/WebClient/MemoryWebClient.cs +++ b/clients/dotnet/WebClient/MemoryWebClient.cs @@ -438,11 +438,18 @@ private async Task ImportInternalAsync( // Add files to the form for (int i = 0; i < uploadRequest.Files.Count; i++) { + using Stream fileStream = uploadRequest.Files[i].FileContent; + + if (fileStream.CanSeek && fileStream.Position > 0) + { + fileStream.Seek(0, SeekOrigin.Begin); + } + string fileName = uploadRequest.Files[i].FileName; byte[] bytes; - using (var binaryReader = new BinaryReader(uploadRequest.Files[i].FileContent)) + using (var binaryReader = new BinaryReader(fileStream)) { - bytes = binaryReader.ReadBytes((int)uploadRequest.Files[i].FileContent.Length); + bytes = binaryReader.ReadBytes((int)fileStream.Length); } var fileContent = new ByteArrayContent(bytes, 0, bytes.Length); From 74940fe186e359f4df0c5680a217ce253734df50 Mon Sep 17 00:00:00 2001 From: David West Date: Fri, 24 May 2024 04:13:55 -0700 Subject: [PATCH 2/2] Update MemoryWebClient.cs --- clients/dotnet/WebClient/MemoryWebClient.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/clients/dotnet/WebClient/MemoryWebClient.cs b/clients/dotnet/WebClient/MemoryWebClient.cs index 8f64a6d95..bfa480fde 100644 --- a/clients/dotnet/WebClient/MemoryWebClient.cs +++ b/clients/dotnet/WebClient/MemoryWebClient.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft. All rights reserved. +// Copyright (c) Microsoft. All rights reserved. using System; using System.Collections.Generic; @@ -438,18 +438,16 @@ private async Task ImportInternalAsync( // Add files to the form for (int i = 0; i < uploadRequest.Files.Count; i++) { - using Stream fileStream = uploadRequest.Files[i].FileContent; - - if (fileStream.CanSeek && fileStream.Position > 0) + if (uploadRequest.Files[i].FileContent is { CanSeek: true, Position: > 0 }) { - fileStream.Seek(0, SeekOrigin.Begin); + uploadRequest.Files[i].FileContent.Seek(0, SeekOrigin.Begin); } string fileName = uploadRequest.Files[i].FileName; byte[] bytes; - using (var binaryReader = new BinaryReader(fileStream)) + using (var binaryReader = new BinaryReader(uploadRequest.Files[i].FileContent)) { - bytes = binaryReader.ReadBytes((int)fileStream.Length); + bytes = binaryReader.ReadBytes((int)uploadRequest.Files[i].FileContent.Length); } var fileContent = new ByteArrayContent(bytes, 0, bytes.Length);