Reputation: 117
how can i upload files larger than 2 gb to my FTP server using powershell, i am using the below function
# Create FTP Rquest Object
$FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
$FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.UseBinary = $true
$FTPRequest.Timeout = -1
$FTPRequest.KeepAlive = $false
$FTPRequest.ReadWriteTimeout = -1
$FTPRequest.UsePassive = $true
# Read the File for Upload
$FileContent = [System.IO.File]::ReadAllBytes(“$LocalFile”)
$FTPRequest.ContentLength = $FileContent.Length
# Get Stream Request by bytes
try{
$Run = $FTPRequest.GetRequestStream()
$Run.Write($FileContent, 0, $FileContent.Length)
# Cleanup
$Run.Close()
$Run.Dispose()
} catch [System.Exception]{
'Upload failed.'
}
i am getting this error while uploading.
Exception calling "ReadAllBytes" with "1" argument(s): "The file is too long.
This operation is currently limited to supporting files less than 2 gigabytes
in size."
+ $FileContent = [System.IO.File]::ReadAllBytes(“$LocalFile”)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException
i have used other functions but the result was so slow upload speed like it was not going higher than 50KB/s hope there is a solution for this, other than splitting the large files into chunks
Upvotes: 2
Views: 1494
Reputation: 117
thanks to PetSerAi and sodawillow
i have found 2 solutions that worked for me.
Solution 1 by : sodawillow
$bufsize = 256mb
$requestStream = $FTPRequest.GetRequestStream()
$fileStream = [System.IO.File]::OpenRead($LocalFile)
$chunk = New-Object byte[] $bufSize
while ( $bytesRead = $fileStream.Read($chunk, 0, $bufsize) ){
$requestStream.write($chunk, 0, $bytesRead)
$requestStream.Flush()
}
$FileStream.Close()
$requestStream.Close()
Solution 2 by : PetSerAi
$FileStream = [System.IO.File]::OpenRead("$LocalFile")
$FTPRequest.ContentLength = $FileStream.Length
$Run = $FTPRequest.GetRequestStream()
$FileStream.CopyTo($Run, 256mb)
$Run.Close()
$FileStream.Close()
Upvotes: 1