2

I wrote a short script that lets me download a file.

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

The whole download is buffered in memory, which is obviously not optimal for larger files. I was wondering (and I couldnt find) a way/ property to prevent this and stream straight to disk. Something along the lines of AllowWriteStreamBuffering in .NET.

Out of Memory Exception When Using HttpWebRequest to Stream Large File

Any help is appreciated!

1 Answer 1

5

Seems the only way to do this is using 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.