【问题标题】:Changing FTP from binary to ascii in PowerShell script using WebClient使用 WebClient 在 PowerShell 脚本中将 FTP 从二进制更改为 ascii
【发布时间】:2015-06-19 20:32:59
【问题描述】:

简单的 PowerShell 脚本。它下载一个文件(二进制)没有问题。我需要它在ASCII中。

$File = "c:\temp\ftpfile.txt"
$ftp = "ftp://myusername:mypass@12.345.6.78/'report'";
$webclient = New-Object -TypeName System.Net.WebClient;
$uri = New-Object -TypeName System.Uri -ArgumentList $ftp;
$webclient.DownloadFile($uri, $File);

【问题讨论】:

  • 源文件是ascii文件吗?它是Unicode吗?还有什么?

标签: .net powershell ftp webclient


【解决方案1】:

WebClient 不支持 ascii/text FTP 模式。

改用FtpWebRequest 并将.UseBinary 设置为false。

$File = "c:\temp\ftpfile.txt"
$ftp = "ftp://myusername:mypass@12.345.6.78/'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)

$responsestream.CopyTo($targetfile)

$targetfile.close()

参考:What's the best way to automate secure FTP in PowerShell?


请注意,WebClient 在内部使用FtpWebRequest,但不会公开其.UseBinary 属性。

【讨论】:

    猜你喜欢
    • 2022-10-05
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多