FtpWebRequest Datei herunterladen
-
03-10-2019 - |
Frage
Der folgende Code soll eine Datei per FTP abzurufen. Allerdings bin ich einen Fehler mit ihm zu bekommen.
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();
}
Der Fehler ist:
Der Remoteserver hat einen Fehler zurückgegeben: (550) Datei nicht verfügbar (zum Beispiel Datei nicht gefunden, kein Zugriff)
Die Datei wird auf jeden Fall gibt es auf dem Remote-Computer, und ich bin in der Lage diese ftp manuell auszuführen (das heißt ich habe Berechtigungen). Kann mir jemand sagen, warum ich diesen Fehler könnte zu bekommen?
Lösung
Dieser Absatz von der FptWebRequest Klassenreferenz Macht von Interesse für Sie:
Die URI kann relativ oder absolut sein. Wenn die URI ist von der Form " ftp://contoso.com/%2fpath " (% 2f ein entflohener ‚/‘), dann ist die URI absolut, und das aktuelle Verzeichnis /Pfad. Wenn jedoch ist die URI des Form " ftp://contoso.com/path ", erster .NET Framework meldet sich der FTP Server (mit dem Benutzernamen und Kennwort Satz von dem Credentials Eigenschaft), dann das aktuelle Verzeichnis eingestellt ist / path.
Andere Tipps
Ich weiß, das ist eine alte Post, aber Ich füge hier für die Zukunft. Hier ist eine Lösung, die ich gefunden:
private void DownloadFileFTP()
{
string inputfilepath = @"C:\Temp\FileName.exe";
string ftphost = "xxx.xx.x.xxx";
string ftpfilepath = "/Updater/Dir1/FileName.exe";
string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
using (WebClient request = new WebClient())
{
request.Credentials = new NetworkCredential("UserName", "P@55w0rd");
byte[] fileData = request.DownloadData(ftpfullpath);
using (FileStream file = File.Create(inputfilepath))
{
file.Write(fileData, 0, fileData.Length);
file.Close();
}
MessageBox.Show("Download Complete");
}
}
Aktualisiert basiert auf ausgezeichneten Vorschlag von Ilya Kogan
Der einfachste Weg,
Die banalsten Art und Weise eine binäre Datei von einem FTP-Server mit .NET-Framework zum Download wird mit WebClient.DownloadFile
:
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");
Erweiterte Optionen
Verwenden Sie FtpWebRequest
nur, wenn Sie brauchen eine bessere Kontrolle, dass WebClient
bietet nicht (wie TLS / SSL-Verschlüsselung , die Überwachung der Fortschritte usw.). Einfache Weise zu kopieren ist nur einen FTP-Antwort-Stream zu FileStream
mit Stream.CopyTo
:
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
ftpStream.CopyTo(fileStream);
}
Fortschrittsüberwachung
Wenn Sie einen Download-Fortschritt überwachen müssen, müssen Sie die Inhalte von Brocken kopieren sich selbst:
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
}
}
Für GUI Fortschritt (WinForms ProgressBar
) finden Sie unter:
FtpWebRequest FTP-Download mit ProgressBar
Download-Ordner
Wenn Sie alle Dateien von einem Remote-Ordner herunterladen möchten, sehen
C # Laden Sie alle Dateien und Unterverzeichnisse über FTP .
Ich hatte das gleiche Problem!
war meine Lösung der public_html
Ordner in die Download-URL einfügen.
Echt Speicherort der Datei auf dem Server:
myhost.com/public_html/myimages/image.png
Web URL:
www.myhost.com/myimages/image.png
private static DataTable ReadFTP_CSV()
{
String ftpserver = "ftp://servername/ImportData/xxxx.csv";
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
// use the stream to read file from FTP
StreamReader sr = new StreamReader(responseStream);
DataTable dt_csvFile = new DataTable();
#region Code
//Add Code Here To Loop txt or CSV file
#endregion
return dt_csvFile;
}
Ich hoffe, dass es Ihnen helfen kann.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
Danach Sie die folgende Zeile zu vermeiden Fehler verwenden können .. (Zugriff verweigert usw.)
request.Proxy = null;
public void download(string remoteFile, string localFile)
{
private string host = "yourhost";
private string user = "username";
private string pass = "passwd";
private FtpWebRequest ftpRequest = null;
private FtpWebResponse ftpResponse = null;
private Stream ftpStream = null;
private int bufferSize = 2048;
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception) { }
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception) { }
return;
}