我需要用来FtpWebRequest将文件放在FTP目录中.在上传之前,我首先想知道这个文件是否存在.
我应该使用什么方法或属性来检查此文件是否存在?
以下代码旨在通过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();
}
Run Code Online (Sandbox Code Playgroud)
错误是:
远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)
该文件肯定存在于远程计算机上,我可以手动执行此ftp(即我有权限).任何人都可以告诉我为什么我可能会收到此错误?
我有一个用.NET 3.5编写的应用程序,它使用FTP从服务器上传/下载文件.该应用程序工作正常,但存在性能问题:
连接FTP服务器需要花费大量时间.FTP服务器位于不同的网络上,并具有Windows 2003 Server(IIS FTP).当多个文件排队等待上传时,从一个文件到另一个文件的更改会使用FTPWebRequest创建一个新连接,并且需要花费很多时间(大约8-10秒).
有可能重新使用连接吗?我不太确定KeepAlive属性.哪些连接保持活跃并重用.
Windows Server 2003上的IIS-FTP不支持SSL,因此任何人都可以通过WireShark等数据包嗅探器轻松查看用户名/密码.我发现如果IIS 7.0,Windows Server 2008在其新版本中支持基于FTP的SSL.
我基本上想要提高我的应用程序的上传/下载性能.任何想法将不胜感激.
**请注意3不是问题,但我希望人们对此发表评论
寻找通过FTP检查给定目录的最佳方法.
目前我有以下代码:
private bool FtpDirectoryExists(string directory, string username, string password)
{
try
{
var request = (FtpWebRequest)WebRequest.Create(directory);
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
return false;
else
return true;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
无论目录是否存在,都返回false.有人能指出我正确的方向.
我有一个C#代码,基本上通过FTP协议上传文件(使用FtpWebRequest).但是,我想在尝试上传文件之前首先确定是否存在可用的互联网连接(因为,如果没有尝试上没有意义,软件应该只是睡一会儿并再次检查) .
有没有一种简单的方法可以做到这一点,或者我只是尝试上传文件,如果失败只是再试一次,假设网络连接已关闭?
我被要求支持隐式和显式FTPS(也称为FTPES).我们目前正在使用.NET FtpWebRequest.是否FtpWebRequest支持两种类型的FTPES,有什么区别?
谢谢
我正在使用上传文件到ftp FtpWebRequest.我需要显示已完成的状态.
到目前为止我的代码是:
public void Upload(string filename, string url)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + url + "/" + fileInf.Name;
FtpWebRequest reqFTP;
//string uri = "ftp://" + Host + "/public_html/testing/blogtest/" + fileInf.Name;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(Username, Password);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive …Run Code Online (Sandbox Code Playgroud) 我想将文件从一台服务器上传到另一台FTP服务器,以下是我上传文件的代码,但是它会抛出一个错误:
远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限).
这是我的代码:
string CompleteDPath = "ftp URL";
string UName = "UserName";
string PWD = "Password";
WebRequest reqObj = WebRequest.Create(CompleteDPath + FileName);
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
reqObj.Credentials = new NetworkCredential(UName, PWD);
FileStream streamObj = System.IO.File.OpenRead(Server.MapPath(FileName));
byte[] buffer = new byte[streamObj.Length + 1];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
reqObj.GetRequestStream().Write(buffer, 0, buffer.Length);
reqObj = null;
Run Code Online (Sandbox Code Playgroud)
你能告诉我哪里出错了吗?
我正在创建一个服务来监视FTP位置以获取新更新,并且需要能够使用WebRequestMethods.Ftp.ListDirectoryDetails方法解析从FtpWebRequest响应返回的响应.如果所有响应都遵循相同的格式,但不同的FTP服务器软件提供不同的响应格式,这将是相当容易的.
例如,可能会返回:
08-10-11 12:02PM <DIR> Version2
06-25-09 02:41PM 144700153 image34.gif
06-25-09 02:51PM 144700153 updates.txt
11-04-10 02:45PM 144700214 digger.tif
Run Code Online (Sandbox Code Playgroud)
另一台服务器可能会返回:
d--x--x--x 2 ftp ftp 4096 Mar 07 2002 bin
-rw-r--r-- 1 ftp ftp 659450 Jun 15 05:07 TEST.TXT
-rw-r--r-- 1 ftp ftp 101786380 Sep 08 2008 TEST03-05.TXT
drwxrwxr-x 2 ftp ftp 4096 May 06 12:24 dropoff
Run Code Online (Sandbox Code Playgroud)
并且还观察到其他差异,因此可能存在一些我尚未遇到的细微差别.
有没有人知道完全托管(不需要访问Windows上的外部DLL)C#类无缝地处理这些情况?
我只需要列出具有以下详细信息的目录内容:文件/目录名称,上次更新或创建的时间戳,文件/目录名称.
提前感谢任何建议,加文
我有一个程序需要在FTP服务器上将文件从一个目录移动到另一个目录.例如,文件位于:
ftp://1.1.1.1/MAIN/Dir1
Run Code Online (Sandbox Code Playgroud)
我需要将文件移动到:
ftp://1.1.1.1/MAIN/Dir2
Run Code Online (Sandbox Code Playgroud)
我发现有几篇文章建议使用Rename命令,所以我尝试了以下内容:
Uri serverFile = new Uri(“ftp://1.1.1.1/MAIN/Dir1/MyFile.txt");
FtpWebRequest reqFTP= (FtpWebRequest)FtpWebRequest.Create(serverFile);
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPass);
reqFTP.RenameTo = “ftp://1.1.1.1/MAIN/Dir2/MyFile.txt";
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用 - 我收到以下错误:
远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限).
起初我认为这可能与权限有关,但据我所知,我拥有整个FTP站点的权限(它在我的本地PC上,并且uri被解析为localhost).
是否可以在这样的目录之间移动文件,如果没有,它怎么可能?
为了解决已提出的一些观点/建议:
另外,我尝试将目录路径设置为:
ftp://1.1.1.1/%2fMAIN/Dir1/MyFile.txt
Run Code Online (Sandbox Code Playgroud)
源路径和目标路径都有 - 但这也没有区别.
我发现这篇文章,似乎说将目标指定为相对路径会有所帮助 - 似乎不可能将绝对路径指定为目标.
reqFTP.RenameTo = “../Dir2/MyFile.txt";
Run Code Online (Sandbox Code Playgroud) ftpwebrequest ×10
c# ×9
ftp ×9
.net ×6
.net-3.5 ×1
asp.net ×1
ftps ×1
progress-bar ×1
winforms ×1