FtpWeb请求下载文件
以下代码旨在通过 FTP 检索文件。但是,我遇到了错误。
serverPath = "ftp://x.x.x.x/tmp/myfile.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
// Read the file from the server & write to destination
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
错误是:
远程服务器返回错误:(550) 文件不可用(例如,文件未找到,无法访问)
该文件确实存在于远程计算机上,并且我能够手动执行此 ftp(即我有权限)。谁能告诉我为什么我可能会收到此错误?
The following code is intended to retrieve a file via FTP. However, I'm getting an error with it.
serverPath = "ftp://x.x.x.x/tmp/myfile.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
// Read the file from the server & write to destination
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
The error is:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access)
The file definitely does exist on the remote machine and I am able to perform this ftp manually (i.e. I have permissions). Can anyone tell me why I might be getting this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道这是一篇旧帖子,但我在这里添加以供将来参考。这是我找到的一个解决方案:
根据 Ilya Kogan 的出色建议进行更新
I know this is an old Post but I am adding here for future reference. Here is a solution that I found:
Updated based upon excellent suggestion by Ilya Kogan
最简单的方法
使用 .NET 框架从 FTP 服务器下载二进制文件的最简单方法是使用
WebClient.DownloadFile
:高级选项
使用
FtpWebRequest
,仅当您需要更大的控制时,WebClient
不提供(如 TLS/SSL 加密、进度监控、ascii/文本传输模式、恢复传输等)。简单的方法是使用Stream.CopyTo
:进度监控
如果需要监控下载进度,则必须自己逐块复制内容:
对于 GUI 进度(WinForms
进度条
),请参阅:FtpWebRequest 使用进度条进行 FTP 下载
下载文件夹
如果您想从远程文件夹下载所有文件,请参阅
C# 通过FTP下载所有文件和子目录。
Easiest way
The most trivial way to download a binary file from an FTP server using .NET framework is using
WebClient.DownloadFile
:Advanced options
Use
FtpWebRequest
, only if you need a greater control, thatWebClient
does not offer (like TLS/SSL encryption, progress monitoring, ascii/text transfer mode, resuming transfers, etc). Easy way is to just copy an FTP response stream toFileStream
usingStream.CopyTo
:Progress monitoring
If you need to monitor a download progress, you have to copy the contents by chunks yourself:
For GUI progress (WinForms
ProgressBar
), see:FtpWebRequest FTP download with ProgressBar
Downloading folder
If you want to download all files from a remote folder, see
C# Download all files and subdirectories through FTP.
FptWebRequest 类参考中的这一段可能会感兴趣给你:
This paragraph from the FptWebRequest class reference might be of interest to you:
我希望它能帮助你。
I hope it can help you.
我有同样的问题!
我的解决方案是将
public_html
文件夹插入到下载 URL 中。服务器上的真实文件位置:
网址:
I had the same issue!
My solution was to insert the
public_html
folder into the download URL.Real file location on the server:
Web URL:
之后,您可以使用以下行来避免错误..(访问被拒绝等)
After this you may use the below line to avoid error..(access denied etc.)
仅供参考,Microsoft 建议不使用 FtpWebRequest 进行新开发:
GitHub 链接指向 此 SO 页面,其中包含第三方 FTP 库的列表,例如 < a href="https://github.com/robinrodricks/FluentFTP" rel="nofollow noreferrer">FluentFTP。
FYI, Microsoft recommends not using FtpWebRequest for new development:
The GitHub link directs to this SO page which contains a list of third-party FTP libraries, such as FluentFTP.