I got the the following error message when uploading. The Powershell create a zip file using 7za.exe
and call my FTP function to upload the file. What may cause the problem? Will Windows ftp.exe client be more stable?
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: $_"
}
}
}