Changing FTP from binary to ascii in PowerShell script using WebClient

后端 未结 1 1489
闹比i
闹比i 2021-01-17 07:12

Simple PowerShell script. It downloads a file (in binary) with no issues. I need it in ascii.

$File = \"c:\\t         


        
相关标签:
1条回答
  • 2021-01-17 07:53

    The WebClient does not support ascii/text FTP mode.

    Use FtpWebRequest instead and set .UseBinary to 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()
    

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


    Note that the WebClient uses the FtpWebRequest internally, but does not expose its .UseBinary property.

    0 讨论(0)
提交回复
热议问题