【问题标题】:Powershell 3: preventing excessive memory usage for webrequestPowershell 3:防止 webrequest 使用过多的内存
【发布时间】:2015-02-02 23:48:09
【问题描述】:

我写了一个简短的脚本,让我可以下载一个文件。

Invoke-WebRequest -Uri $r2.Forms.Action -WebSession $myWebSession -OutFile $destination$filename

整个下载都缓冲在内存中,这对于较大的文件显然不是最佳选择。我想知道(我找不到)一种方法/属性来防止这种情况并直接流到磁盘。类似于 .NET 中的 AllowWriteStreamBuffering。

Out of Memory Exception When Using HttpWebRequest to Stream Large File

感谢任何帮助!

【问题讨论】:

    标签: powershell powershell-3.0


    【解决方案1】:

    似乎唯一的方法是使用 WebClient

    $webclient = new-object System.Net.WebClient
    $webclient.Credentials = new-object System.Net.NetworkCredential($id, $p, $url)
    $webclient.DownloadFile($r2, $destination+$tempfile)
    
    #rename tempfile
    $filename = $webclient.ResponseHeaders["Content-Disposition"].SubString($webclient.ResponseHeaders["Content-Disposition"].IndexOf("filename=")+10).Replace("`"", "");
    Rename-Item $destination$tempfile -NewName $filename
    

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      相关资源
      最近更新 更多