WebClient
不支持ascii/text FTP模式。
改为使用FtpWebRequest
,并将.UseBinary
设置为false。
$File = "c:\temp\ftpfile.txt"
$ftp = "ftp://myusername:[email protected]/'report'";
$ftprequest = [System.Net.FtpWebRequest]::Create($ftp)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $false
$ftpresponse = $ftprequest.GetResponse()
$responsestream = $ftpresponse.GetResponseStream()
$targetfile = New-Object IO.FileStream($File, [IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024
do
{
$readlength = $responsestream.Read($readbuffer, 0, 1024)
$targetfile.Write($readbuffer, 0, $readlength)
}
while ($readlength -ne 0)
$targetfile.close()
参考:What's the best way to automate secure FTP in PowerShell?
注意,WebClient
内部使用的FtpWebRequest
,但不公开其.UseBinary
财产。
源文件是否在ascii中?它在Unicode中吗?还有别的吗? –