在Windows命令提示符/ PowerShell中下载大文件

我现在已经没有选择……

尝试1

在Powershell中使用iwr.它工作,显示进度,但它的速度慢10倍,并且直到文件在内存中时才会刷新:(.

 powershell -command "& { iwr https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -OutFile SUV.zip }"

尝试2

在Powershell中使用.Net webclient.它工作,但没有显示进展,你不能通过Ctrl C终止:(.最后一个问题是一个很大的挫折.

powershell -command "& { (New-Object System.Net.WebClient).DownloadFile('https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip', 'SUV.zip') }"

尝试3

在Powershell中使用BITS传输.它工作,显示进度,几乎完美…直到你发现它mysteriously doesn’t work on GitHub(403禁止错误)!

powershell -command "& { Start-BitsTransfer -Source https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -Destination SUV.zip }"

最佳答案 不确定我从哪里获得这段代码,但我已经修改了好几次.希望这会帮助你.

function downloadFile($url, $targetFile)
{
    "Downloading $url"
    $uri = New-Object "System.Uri" "$url"
    $request = [System.Net.HttpWebRequest]::Create($uri)
    $request.set_Timeout(15000) #15 second timeout
    $response = $request.GetResponse()
    $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
    $responseStream = $response.GetResponseStream()
    $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
    $buffer = new-object byte[] 10KB
    $count = $responseStream.Read($buffer,0,$buffer.length)
    $downloadedBytes = $count
    while ($count -gt 0)
    {
        [System.Console]::CursorLeft = 0
        [System.Console]::Write("Downloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength)
        $targetStream.Write($buffer, 0, $count)
        $count = $responseStream.Read($buffer,0,$buffer.length)
        $downloadedBytes = $downloadedBytes + $count
    }
    "Finished Download"
    $targetStream.Flush()
    $targetStream.Close()
    $targetStream.Dispose()
    $responseStream.Dispose()
}

downloadFile "http://URL_to_your_file" "C:\Path\to\destination.file"
点赞