2010-05-06 24 views
27

निम्न कोड एफ़टीपी के माध्यम से एक फ़ाइल को पुनर्प्राप्त करने के लिए है। हालांकि, मुझे इसके साथ एक त्रुटि मिल रही है।FtpWebRequest फ़ाइल डाउनलोड करें

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 remote server returned an error: (550) File unavailable (e.g., file not found, no access)

फ़ाइल निश्चित रूप से रिमोट मशीन पर मौजूद है और मैं मैन्युअल रूप से इस एफ़टीपी प्रदर्शन करने में सक्षम हूँ (अर्थात मैं अनुमतियाँ)। क्या कोई मुझे बता सकता है कि मुझे यह त्रुटि क्यों मिल रही है?

+1

मुझे इस तरह की चीजों के लिए वायरसहार्क उपयोगी लगता है। आप अपनी मशीन और सर्वर के बीच एफ़टीपी यातायात देखने के लिए एक फ़िल्टर सेट अप कर सकते हैं। –

+0

यदि आप UsePassive को झूठी पर सेट करते हैं तो क्या होता है? मुझे कभी भी निष्क्रिय मोड का उपयोग कर कोई सर्वर काम नहीं मिला .. – Roy

+0

यह आमतौर पर मेरे अनुभव में त्रुटि का कारण बनता है क्योंकि यह फ़ायरवॉल द्वारा अवरुद्ध बंदरगाह का उपयोग करने का प्रयास करता है। –

उत्तर

21

FptWebRequest class reference से इस पैरा आपकी रुचि का हो सकता है:

The URI may be relative or absolute. If the URI is of the form " ftp://contoso.com/%2fpath " (%2f is an escaped '/'), then the URI is absolute, and the current directory is /path. If, however, the URI is of the form " ftp://contoso.com/path ", first the .NET Framework logs into the FTP server (using the user name and password set by the Credentials property), then the current directory is set to /path.

+1

और यह कैसे मदद कर सकता है? – SerG

+0

ठीक है, मेरे लिए यह गैर-ASCII वर्णों से निपटने का विषय था, जैसे कि # यूआरएल में था, उन्हें यूआरएल एन्कोड किया जाना चाहिए। – CularBytes

40

मैं जानता हूँ कि यह एक पुरानी पोस्ट है, लेकिन मैं यहाँ भविष्य में संदर्भ के द्वारा जोड़ा जा रहा।

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", "[email protected]"); 
      byte[] fileData = request.DownloadData(ftpfullpath); 

      using (FileStream file = File.Create(inputfilepath)) 
      { 
       file.Write(fileData, 0, fileData.Length); 
       file.Close(); 
      } 
      MessageBox.Show("Download Complete"); 
     } 
    } 

उत्कृष्ट सुझाव पर इल्या कोगन द्वारा आधारित अपडेट किया गया

+3

ध्यान दें कि आपको IDISposable ऑब्जेक्ट्स का निपटान करना चाहिए। ऐसा करने का सबसे आसान तरीका कीवर्ड 'उपयोग' का उपयोग करना है। –

+0

आप सही हैं, मैंने इस प्रतिक्रिया को पोस्ट किया जब मैं सी # –

+9

के बजाय नया था, यदि आप 'FtpWebRequest' की बजाय 'WebClient' का उपयोग करने जा रहे हैं, तो आप इसकी [' DownloadFile'] (http: // msdn.microsoft.com/en-us/library/system.net.webclient.downloadfile.aspx) विधि, 'फ़ाइलस्ट्रीम' के साथ गड़बड़ करने की बजाय, जो थोड़ा आसान हो सकता है। ऐसी कुछ चीजें हैं जो वेब क्लाइंट नहीं कर सकती हैं, हालांकि (जैसे 'पीएएसवी' एफ़टीपी के बजाय 'ACTV' का उपयोग करें:' FtpWebRequest.UsePassive = false; ') –

2

मैं एक ही मुद्दा था: यहाँ एक समाधान है कि मैंने पाया है!

मेरा समाधान डाउनलोड यूआरएल में public_html फ़ोल्डर डालना था। सर्वर पर

रियल फ़ाइल स्थान:

myhost.com/public_html/myimages/image.png

वेब यूआरएल:

www.myhost.com/myimages/image.png

0
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath); 

इस के बाद आप त्रुटि से बचने के लिए .. (पहुँच नीचे लाइन का उपयोग कर सकते अस्वीकार इत्यादि)

request.Proxy = null; 
+0

प्रॉक्सी डिफ़ॉल्ट रूप से कुछ भी नहीं है। –

1
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; 

    } 

मुझे आशा है कि यह आप कर सकते हैं।

0
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; 
    } 
4

नेट ढांचे का उपयोग कर एक FTP सर्वर से एक बाइनरी फ़ाइल डाउनलोड करने के लिए सबसे छोटी रास्ता 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"); 

उपयोग FtpWebRequest, केवल यदि आप एक अधिक से अधिक नियंत्रण की जरूरत है, कि WebClient नहीं है प्रस्ताव (जैसे टीएलएस/एसएसएल एन्क्रिप्शन, प्रगति निगरानी आदि)।आसान तरीका सिर्फ FileStream को एक FTP प्रतिक्रिया स्ट्रीम कॉपी करने के लिए 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")) 
{ 
    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); 
    } 
} 

जीयूआई के लिए:

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); 
} 

आप एक डाउनलोड प्रगति की निगरानी करने की जरूरत है, तो आप अपने आप को हिस्सा द्वारा सामग्री की प्रतिलिपि करने के लिए है प्रगति (WinForms ProgressBar), देखें:
FtpWebRequest FTP download with ProgressBar

आप किसी दूरस्थ फ़ोल्डर से सभी फाइलों को डाउनलोड करने के लिए चाहते हैं,
01 देखनाC# Download all files and subdirectories through FTP

संबंधित मुद्दे