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中嗎?還有別的嗎? –