2017-01-15 72 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