简体   繁体   中英

can't upload files larger than 2 gb over FTP - Powershell

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM