2017-01-15 66 views
1

如何上传大于2GB的文件到我的FTP服务器使用PowerShell,我使用下面的函数不能上传文件大于2 GB,通过FTP - PowerShell的

# 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的 希望有一个解决方案,其他

+2

的[Powershell的读取块文件(可能的复制http://stackoverflow.com/问题/ 39345335/powershell-read-file-in-chunk) – sodawillow

+0

我将检查出现在.. – Vagho

+1

'$ FileStream = [System.IO.File] :: OpenRead(“$ LocalFil E”); $ FTPRequest.ContentLength = $ FileStream.Length; $ Run = $ FTPRequest.GetRequestStream(); $ FileStream.CopyTo($运行); $ Run.Close(); $ FileStream.Close();' – PetSerAl

回答

0

感谢PetSerAisodawillow

我找到了2个解决方案,为我工作。

解决方案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() 
+2

您可以增加缓冲区大小以复制'$ FileStream.CopyTo($ Run,256mb)'流。这应该会让它变得更快。 – PetSerAl

+0

我会尽力:)谢谢。 – Vagho