Method invocation failed because [Microsoft.SharePoint.Client.File] does not contain a method named StartUpload

Recently while working on uploading large files to SharePoint online document library using PowerShell in SharePoint online, I got the below error which says: Method invocation failed because [Microsoft.SharePoint.Client.File] does not contain a method named ‘StartUpload’

Method invocation failed because [Microsoft.SharePoint.Client.File] does not contain a method named StartUpload
Method invocation failed because [Microsoft.SharePoint.Client.File] does not contain a method named StartUpload

Method invocation failed because [Microsoft.SharePoint.Client.File] does not contain a method named StartUpload

Below is the code we can write using Visual Studio Code or Windows PowerShell ISE.

$ContentStream = New-Object System.IO.MemoryStream

# Add an empty file.
$fileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileInfo.ContentStream = $ContentStream
$fileInfo.Url = $UniqueFileName
$fileInfo.Overwrite = $true
$Upload = $Docs.RootFolder.Files.Add($fileInfo)
$ctx.Load($Upload)

# Start upload by uploading the first slice.

$s = [System.IO.MemoryStream]::new($buffer)

# Call the start upload method on the first slice.
$BytesUploaded = $Upload.StartUpload($UploadId, $s)
$ctx.ExecuteQuery()

The error was coming in the below line:

$BytesUploaded = $Upload.StartUpload($UploadId, $s)

Read some SharePoint errors and solutions:

Here I was using the 15 versions of Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll. Here you need to use the 16 versions of both the dlls. Once I replaced with updated 16 versions of client dlls, it worked fine.

>