diff --git a/dotnet/src/webdriver/Remote/RemoteWebDriver.cs b/dotnet/src/webdriver/Remote/RemoteWebDriver.cs index 35d0b62ebf3cd..d0688ac2ad600 100644 --- a/dotnet/src/webdriver/Remote/RemoteWebDriver.cs +++ b/dotnet/src/webdriver/Remote/RemoteWebDriver.cs @@ -477,7 +477,8 @@ public DevToolsSession GetDevToolsSession(int protocolVersion) public IReadOnlyList GetDownloadableFiles() { var enableDownloads = this.Capabilities.GetCapability(CapabilityType.EnableDownloads); - if (enableDownloads == null || !(bool) enableDownloads) { + if (enableDownloads == null || !(bool)enableDownloads) + { throw new WebDriverException("You must enable downloads in order to work with downloadable files."); } @@ -494,7 +495,8 @@ public IReadOnlyList GetDownloadableFiles() public void DownloadFile(string fileName, string targetDirectory) { var enableDownloads = this.Capabilities.GetCapability(CapabilityType.EnableDownloads); - if (enableDownloads == null || !(bool) enableDownloads) { + if (enableDownloads == null || !(bool)enableDownloads) + { throw new WebDriverException("You must enable downloads in order to work with downloadable files."); } @@ -508,20 +510,19 @@ public void DownloadFile(string fileName, string targetDirectory) byte[] fileData = Convert.FromBase64String(contents); Directory.CreateDirectory(targetDirectory); - string tempFile = Path.Combine(targetDirectory, "temp.zip"); - File.WriteAllBytes(tempFile, fileData); - using (ZipArchive archive = ZipFile.OpenRead(tempFile)) + using (var memoryReader = new MemoryStream(fileData)) { - foreach (ZipArchiveEntry entry in archive.Entries) + using (var zipArchive = new ZipArchive(memoryReader, ZipArchiveMode.Read)) { - string destinationPath = Path.Combine(targetDirectory, entry.FullName); + foreach (ZipArchiveEntry entry in zipArchive.Entries) + { + string destinationPath = Path.Combine(targetDirectory, entry.FullName); - entry.ExtractToFile(destinationPath); + entry.ExtractToFile(destinationPath); + } } } - - File.Delete(tempFile); } /// @@ -530,7 +531,8 @@ public void DownloadFile(string fileName, string targetDirectory) public void DeleteDownloadableFiles() { var enableDownloads = this.Capabilities.GetCapability(CapabilityType.EnableDownloads); - if (enableDownloads == null || !(bool) enableDownloads) { + if (enableDownloads == null || !(bool)enableDownloads) + { throw new WebDriverException("You must enable downloads in order to work with downloadable files."); }