我已经建立了一个脚本,用于从FTP服务器下载文件。该脚本适用于我尝试下载的所有文件,但包含的文件除外#
。经过研究后,我无法找到该问题的解决方案。下面列出了我用于下载文件的代码。
function Get-FtpFile
{
Param ([string]$fileUrl, $credentials, [string]$destination)
try
{
$FTPRequest = [System.Net.FtpWebRequest]::Create($fileUrl)
if ($credentials)
{
$FTPRequest.Credentials = $credentials
}
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFile = New-Object IO.FileStream ($destination,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$LocalFile.Close()
}
catch [Net.WebException]
{
return "Unable to download because: $($_.exception)"
}
}
我尝试使用WebRequest.DownloadFile()
代替,但它仍然不适用于包含的文件#
,我也尝试过使用FtpWebRequest
重命名方法重命名文件,但也无法使用。
有人知道该问题的任何解决方案或解决方法吗?
您必须对URL中的#
as进行URL编码%23
($fileUrl
)。
如果要以编程方式进行操作,请参阅:
从C#的FTP服务器下载名称包括特殊字符的文件
在PowerShell中,它是这样的:
Add-Type -AssemblyName System.Web
$fileUrl = "https://example.com/path/" + [System.Web.HttpUtility]::UrlEncode($filename)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句