1

I'm trying a code from:
Upload files with FTP using PowerShell

$request = [Net.WebRequest]::Create("ftp://ftp.example.com/remote/path/file.zip")
$request.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile 

$fileStream = [System.IO.File]::OpenRead("C:\local\path\file.zip")
$ftpStream = $request.GetRequestStream()

$fileStream.CopyTo($ftpStream)

$ftpStream.Dispose()
$fileStream.Dispose()

And I get this error:

Error in Calling this method: [System.IO.FileStream] doesn't contain a method named "CopyTo".

Do you have an idea?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Jan987321
  • 11
  • 3

1 Answers1

0

For Stream.CopyTo, you need .NET framework 4.

  • Either upgrade your .NET framework installation
  • Or use a loop, as you can see in the "Progress monitoring" section of my answer to:
    Upload files with FTP using PowerShell

    $buffer = New-Object Byte[] 10240
    while (($read = $fileStream.Read($buffer, 0, $buffer.Length)) -gt 0)
    {
        $ftpStream.Write($buffer, 0, $read)
    }
    
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992