Tag: ftpwebrequest

当前用户目录与root不同时,无法使用ftp方法重命名文件

备注:由于垃圾邮件防范机制,我被迫将Uris的开头从ftp://替换为ftp。 我有以下问题。 我必须使用C#ftp方法上传文件,然后重命名它。 容易,对吗? 🙂 好吧,假设我的ftp主机是这样的: ftp.contoso.com 登录后,当前目录设置为: 用户/名称 所以,我想要实现的是登录,将文件作为file.ext.tmp上传到当前目录,上传成功后,将文件重命名为file.ext 正如我猜测的那样,整个难题是为FtpWebRequest正确设置请求Uri。 MSDN声明: URI可以是相对的或绝对的。 如果URI的格式为“ ftp://contoso.com/%2fpath”(%2f是转义’/’),那么URI是绝对的,当前目录是/ path。 但是,如果URI的格式为“ ftp://contoso.com/path ”,则首先.NET Framework登录到FTP服务器(使用Credentials属性设置的用户名和密码),然后是当前目录设置为UserLoginDirectory / path。 好的,所以我使用以下URI上传文件: ftp.contoso.com/file.ext.tmp 好的,文件落在我想要的位置:在目录“users / name”中 现在,我想重命名该文件,因此我使用以下Uri创建Web请求: ftp.contoso.com/file.ext.tmp 并指定重命名为参数: file.ext 这给了我550错误:找不到文件,没有权限等。 我在Microsoft网络监视器中跟踪了它,它给了我: 命令:RNFR,重命名自 CommandParameter:/file.ext.tmp Ftp:响应端口53724,’550文件/file.ext.tmp未找到’ 好像是在根目录中查找文件 – 而不是在当前目录中。 我使用Total Commander手动重命名了文件,唯一的区别是CommandParameter没有第一个斜杠: CommandParameter:file.ext.tmp 我可以通过提供以下绝对URI来成功重命名该文件: ftp.contoso.com/%2fusers/%2fname/file.ext.tmp 但我不喜欢这种方法,因为我必须知道当前用户目录的名称。 它可以通过使用WebRequestMethods.Ftp.PrintWorkingDirectory来完成,但它增加了额外的复杂性(调用此方法来检索目录名称,然后组合路径以形成正确的URI)。 我不明白为什么URI ftp.contoso.com/file.ext.tmp适合上传而不是重命名? 我在这里错过了什么吗? 该项目设置为.NET 4.0,在Visual Studio 2010中编码。 编辑 好的,我放置了代码片段。 […]

服务器返回一个地址以响应PASV命令,该命令与进行FTP连接的地址不同

System.Net.WebException:服务器返回一个地址以响应PASV命令,该命令与进行FTP连接的地址不同。 在System.Net.FtpWebRequest.CheckError() 在System.Net.FtpWebRequest.SyncRequestCallback(Object obj) 在System.Net.CommandStream.Abort(例外e) 在System.Net.FtpWebRequest.FinishRequestStage(RequestStage阶段) 在System.Net.FtpWebRequest.GetRequestStream() 在D:\ PROJEKTI \ BackupDB \ BackupDB \ Program.cs中的BackupDB.Program.FTPUploadFile(String serverPath,String serverFile,FileInfo LocalFile,NetworkCredential Cred):第119行 码: FTPMakeDir(new Uri(serverPath + “/”), Cred); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath+serverFile); request.UsePassive = true; request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = Cred; byte[] buffer = new byte[10240]; // Read/write 10kb using (FileStream sourceStream = new FileStream( LocalFile.ToString(), FileMode.Open)) { […]

将文件上传到FTP在目标中已损坏一次

我正在创建一个简单的drag-file-and-upload-automatic-to-ftp windows应用程序 我正在使用MSDN代码将文件上传到FTP。 代码很简单: // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(String.Format(“{0}{1}”, FTP_PATH, filenameToUpload)); request.Method = WebRequestMethods.Ftp.UploadFile; // Options request.UseBinary = true; request.UsePassive = false; // FTP Credentials request.Credentials = new NetworkCredential(FTP_USR, FTP_PWD); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader(fileToUpload.FullName); […]

上传到FTP并使用FtpWebRequest后退后,存档或映像已损坏

我有两种方法: 将文件上载到FTP服务器 从服务器下载文件。 一切都与文本或xml文件完美配合。 但是,当我尝试上传然后下载存档或图像时,我得到“Windows无法打开文件夹。压缩的zip文件无效”,档案错误,图像几乎相同。 可能是什么问题? 这是我的方法列表: 上传: private string Upload(string Login, string Password, string FilePath, string FileName, string uuid, string FTPDir) { string CreateDirectory = CreateFTPDirectory(Login, Password, uuid, FTPDir); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@”ftp://” + FTPDir + uuid + “/” + FileName); request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; StreamReader sourceStream = new StreamReader(FilePath + FileName); […]

如何在中断互联网后继续或恢复FTP上传

我使用下面的代码(C#.NET 3.5)上传文件: FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“ftp://someweb.mn/altanzulpharm/file12.zip”); request.Method = WebRequestMethods.Ftp.UploadFile; request.KeepAlive = true; request.UseBinary = true; request.Credentials = new NetworkCredential(username, password); FileStream fs = File.OpenRead(FilePath); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); Stream ftpstream = request.GetRequestStream(); ftpstream.Write(buffer, 0, buffer.Length); ftpstream.Close(); 但是当互联网中断时,上传会中断。 中断发生的时间非常短,几乎是一毫秒。 但永远上传rest! 互联网中断后是否可以继续或继续上传?

FtpWebRequest下载文件

以下代码旨在通过FTP检索文件。 但是,我收到了一个错误。 serverPath = “ftp://xxxx/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 […]

如何使用FTP在目录之间移动文件?

我有一个程序需要在FTP服务器上将文件从一个目录移动到另一个目录。 例如,文件位于: ftp://1.1.1.1/MAIN/Dir1 我需要将文件移动到: ftp://1.1.1.1/MAIN/Dir2 我发现有几篇文章建议使用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(); 但这似乎不起作用 – 我收到以下错误: 远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)。 起初我认为这可能与权限有关,但据我所知,我拥有整个FTP站点的权限(它在我的本地PC上,并且uri被解析为localhost)。 是否可以在这样的目录之间移动文件,如果没有,它怎么可能? 为了解决已经提出的一些观点/建议: 我可以从源目录下载相同的文件,所以它肯定存在(我正在做的是首先下载文件,然后将其移动到其他地方)。 我可以从浏览器(源目录和目标目录)访问ftp站点 ftp服务器在我本地计算机上的自己的IIS实例下运行。 路径和大小写是正确的,没有特殊字符。 另外,我尝试将目录路径设置为: ftp://1.1.1.1/%2fMAIN/Dir1/MyFile.txt 源路径和目标路径都有 – 但这也没有区别。 我发现这篇文章,似乎说将目标指定为相对路径会有所帮助 – 似乎不可能将绝对路径指定为目标。 reqFTP.RenameTo = “../Dir2/MyFile.txt”;

使用ProgressBar进行FtpWebRequest FTP下载

我的代码有效,但ProgressBar直接跳转到100%,下载将继续。 当它完成后,会出现一个消息框来获取信息。 我已经改变了缓冲区大小,但没关系。 我在这做错了什么? 这是我的代码: void workerDOWN_DoWork(object sender, DoWorkEventArgs e) { string fileFullPath = e.Argument as String; string fileName = Path.GetFileName(fileFullPath); string fileExtension = Path.GetExtension(fileName); label4.Invoke((MethodInvoker)delegate { label4.Text = “Downloading File..”; }); string ftpServerIP = “XXX”; string ftpUserName = “XXX”; string ftpPassword = “XXX”; try { //Datei vom FTP Server downloaden FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“ftp://” […]

如何在C#中发送任意FTP命令

我已经使用C#中的FtpWebRequest类实现了上传,下载,删除等function。 这是相当直接的。 我现在需要做的是支持发送任意FTP命令,如 quote SITE LRECL=132 RECFM=FB or quote SYST 以下是我们的app.config直接配置示例: quote SITE LRECL=132 RECFM=FB 我还在研究如何使用FtpWebRequest来做到这一点。 我接下来可能会尝试WebClient类。 任何人都可以更快地指出我正确的方向吗? 谢谢! 更新:我得出了同样的结论,因为.NET Framework 3.5 FtpWebRequest不支持WebRequestMethods.Ftp.*任何内容。 我会尝试一些其他post推荐的第三方应用。 谢谢您的帮助!

如何检查FTP目录是否存在

寻找通过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; } 无论目录是否存在,都返回false。 有人能指出我正确的方向。