无法通过FTP上传大于2 GB的文件 - Powershell

时间:2017-01-15 09:42:14

标签: powershell ftp

如何使用PowerShell将大于2 gb的文件上传到我的FTP服务器,我正在使用以下功能

# 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.'       
}

我在上传时遇到此错误。

 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

我已经使用了其他功能,但结果上传速度非常慢,因为它没有超过50KB / s 希望有一个解决方案,除了将大文件拆分成块

1 个答案:

答案 0 :(得分:0)

感谢PetSerAisodawillow

我找到了两个对我有用的解决方案。

解决方案1: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()

解决方案2:PetSerAi

    $FileStream = [System.IO.File]::OpenRead("$LocalFile")
    $FTPRequest.ContentLength = $FileStream.Length
    $Run = $FTPRequest.GetRequestStream()
    $FileStream.CopyTo($Run, 256mb)
    $Run.Close()
    $FileStream.Close()
相关问题