在上载时,我收到以下错误信息。 Powerhell使用7za.exe
创建了一个齐p文档,并称我的FLTP功能是上载文件。 可能引起这一问题的是什么? Windows ftp。 外部客户更加稳定?
Exception calling "GetRequestStream" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
Update:
It seems the same files always failed in the loop. However, It works if I just run ftpFile file_name_with_full_path
. (The file_name_with_full_path
is copied from the output of the loop script.
Update 2:
I tried to use webclient
($webclient.UploadFile($uri, $File))
to ftp the files. Same error.
Update 3:
Found this Question. May need to add $ftp.KeepAlive = false
. Why?
function ftpFile
{
Param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateScript({Test-Path $_})]
[String]
$filePath
,
[Parameter(Mandatory=$false)]
[String]
$ftpUrl = "ftp://10.0.1.1/Data/"
,
[Parameter(Mandatory=$false)]
[String]
$Login = "username"
,
[Parameter(Mandatory=$false)]
[String]
$password = "password"
)
Process {
try {
$ftp = [System.Net.FtpWebRequest]::Create("$ftpUrl/$(Split-Path $filePath -Leaf)")
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential("$Login","$password")
$ftp.UseBinary = $true
$ftp.UsePassive = $true
# read in the file to upload as a byte array
$content = gc -en byte $filePath
$ftp.ContentLength = $content.Length
# get the request stream, and write the bytes into it
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
$rs.Close()
$rs.Dispose()
echo "ftpFile: $filePath size: $($content.Length)"
}
catch {
throw "FTP: $_"
}
}
}