使用 FtpWebRequest 从远程服务器下载的文件内容在记事本中显示为一行
Contents of file downloaded from remote server with FtpWebRequest is shown in one line in Notepad
我正在使用 PowerShell 从远程服务器复制文件并保存在我的本地计算机中。但是当我在记事本中打开该文件时从远程服务器复制文件后,它没有以正确的格式(对齐)打开。但是如果我使用 FTP 命令手动复制,它在记事本中的对齐方式是正确的。
请找到我的 PowerShell 脚本:
$File = "D:\copiedfile.txt"
$ftp = "ftp://remote_machine_name//tmp/text.txt"
$ftprequest = [System.Net.FtpWebRequest]::Create($ftp)
$ftprequest.UseBinary = $true
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Downloading $File..."
$webclient.DownloadFile($uri, $File)
使用 PowerShell 脚本(未正确对齐)复制文件后,请查看随附的屏幕截图。
请在使用 FTP 手动复制文件(正确对齐)后找到附加的屏幕截图。
由于跨平台,我遇到了这个对齐问题。正在将文件从 HP-UX 复制到 Windows。不确定,如何解决这个问题。
当我通过 FTP 手动(命令行)复制文件时,其传输模式为 ASCII。但是我不确定如何在我的 powershell 脚本中为 ASCII 设置传输模式。
Windows记事本仅支持WindowsEOL。您的文件很可能有 *nix EOL。
您需要使用 ascii/text 模式,而不是二进制,以便 FtpWebRequest
可以将文件转换为 Windows EOL。
$ftprequest.UseBinary = $False
尽管请注意,当您创建 FtpWebRequest
时,您从未真正使用过它。
完整代码如下:
$url = "ftp://remote_machine_name//tmp/text.txt"
$ftprequest = [System.Net.FtpWebRequest]::Create($url)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $false
$ftpresponse = $ftprequest.GetResponse()
$responsestream = $ftpresponse.GetResponseStream()
$localPath = "D:\copiedfile.txt"
$targetfile = New-Object IO.FileStream($localPath, [IO.FileMode]::Create)
$responsestream.CopyTo($targetfile);
$responsestream.Close()
$targetfile.Close()
(并删除所有 WebClient
代码)
Stream.CopyTo
was added in .NET 4. If you need to use an older version of .NET, you need to copy stream contents in a loop, as shown for example in .
它与 command-line ftp
一起正常工作,因为它默认为 ascii/text 模式。
我正在使用 PowerShell 从远程服务器复制文件并保存在我的本地计算机中。但是当我在记事本中打开该文件时从远程服务器复制文件后,它没有以正确的格式(对齐)打开。但是如果我使用 FTP 命令手动复制,它在记事本中的对齐方式是正确的。
请找到我的 PowerShell 脚本:
$File = "D:\copiedfile.txt"
$ftp = "ftp://remote_machine_name//tmp/text.txt"
$ftprequest = [System.Net.FtpWebRequest]::Create($ftp)
$ftprequest.UseBinary = $true
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Downloading $File..."
$webclient.DownloadFile($uri, $File)
使用 PowerShell 脚本(未正确对齐)复制文件后,请查看随附的屏幕截图。
请在使用 FTP 手动复制文件(正确对齐)后找到附加的屏幕截图。
由于跨平台,我遇到了这个对齐问题。正在将文件从 HP-UX 复制到 Windows。不确定,如何解决这个问题。
当我通过 FTP 手动(命令行)复制文件时,其传输模式为 ASCII。但是我不确定如何在我的 powershell 脚本中为 ASCII 设置传输模式。
Windows记事本仅支持WindowsEOL。您的文件很可能有 *nix EOL。
您需要使用 ascii/text 模式,而不是二进制,以便 FtpWebRequest
可以将文件转换为 Windows EOL。
$ftprequest.UseBinary = $False
尽管请注意,当您创建 FtpWebRequest
时,您从未真正使用过它。
完整代码如下:
$url = "ftp://remote_machine_name//tmp/text.txt"
$ftprequest = [System.Net.FtpWebRequest]::Create($url)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $false
$ftpresponse = $ftprequest.GetResponse()
$responsestream = $ftpresponse.GetResponseStream()
$localPath = "D:\copiedfile.txt"
$targetfile = New-Object IO.FileStream($localPath, [IO.FileMode]::Create)
$responsestream.CopyTo($targetfile);
$responsestream.Close()
$targetfile.Close()
(并删除所有 WebClient
代码)
Stream.CopyTo
was added in .NET 4. If you need to use an older version of .NET, you need to copy stream contents in a loop, as shown for example in
它与 command-line ftp
一起正常工作,因为它默认为 ascii/text 模式。