简体   繁体   中英

File appears corrupted when it's downloaded with System.Net.WebClient.DownloadFile

I uploaded a file to a cloud which gives me direct download link.

Downloading it by clicking on this link works fine, but when I try to download it via System.Net.WebClient.DownloadFile on Powershell, it downloads the file, but when I open it it says that the file is corrupted and unreadable .

That's the code:

$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile("https://xxxxxx.com/xxxxx/xxx.exe","C:\Users\user\Desktop\xxx.exe")

Any solution?

Strange, this logic works for me.

You could try adding $WebClient.Dispose() ?

or other PowerShell download methods such as:

$uri = "https://xxxxxx.com/xxxxx/xxx.exe"
$path = "C:\Users\user\Desktop\xxx.exe"

Invoke-WebRequest -Uri $uri -OutFile $path

3 ways to download files with PowerShell

<#
# 1. Invoke-WebRequest

The first and most obvious option is the Invoke-WebRequest cmdlet. It is built
into PowerShell and can be used in the following method:
#>

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
Invoke-WebRequest -Uri $url -OutFile $output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

<#
2. System.Net.WebClient

A common .NET class used for downloading files is the System.Net.WebClient class.
#>

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)

# OR

(New-Object System.Net.WebClient).DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

<#
3. Start-BitsTransfer

If you haven't heard of BITS before, check this out. BITS is primarily designed
for asynchronous file downloads, but works perfectly fine synchronously too
(assuming you have BITS enabled).
#>

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $output

# Or

Start-BitsTransfer -Source $url -Destination $output -Asynchronous
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

Here is what I personally use daily, from a function in my personal module imported via my profile.

$webclient = New-Object System.Net.WebClient
$url       = 'https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe'

$filename = [System.IO.Path]::GetFileName($url)
$file     = "$TechToolsUNC\$filename"

$webclient.DownloadFile($url,$file)
Start-Process $file -Wait

I would speculate that his is not about powershell, but other factors on your machine or network, most likely antivirus agent etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM