【发布时间】:2017-01-15 09:42:13
【问题描述】:
如何使用 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 希望有一个解决方案,而不是将大文件分成块
【问题讨论】:
-
我现在去看看..
-
$FileStream = [System.IO.File]::OpenRead(“$LocalFile”); $FTPRequest.ContentLength = $FileStream.Length; $Run = $FTPRequest.GetRequestStream(); $FileStream.CopyTo($Run); $Run.Close(); $FileStream.Close(); -
你的意思是这样吗? $FileStream = [System.IO.File]::OpenRead(“$LocalFile”); $FTPRequest.ContentLength = $FileStream.Length; $Run = $FTPRequest.GetRequestStream(); $FileStream.CopyTo($Run); $Run.Write($FileStream, 0, $FileStream.Length); $Run.Close() $FileStream.Close();
-
@Vagho 是的,像这样,但没有
$Run.Write($FileStream, 0, $FileStream.Length);。
标签: powershell ftp