टिप्पणी: स्पैम रोकथाम तंत्र के कारण मुझे यूआरआई की शुरुआत को ftp: // से ftp से बदलने के लिए मजबूर होना पड़ा।वर्तमान उपयोगकर्ता निर्देशिका रूट से भिन्न है
मुझे निम्नलिखित समस्या मिली है। मुझे सी # एफटीपी विधि के साथ फाइल अपलोड करनी है और बाद में इसका नाम बदलें। आसान, सही?
ftp.contoso.com
और प्रवेश करने के बाद, वर्तमान निर्देशिका के लिए सेट है:
users/name
तो, क्या मैं कोशिश कर रहा हूँ :)
ठीक है, चलो कहते हैं कि मेरी एफ़टीपी मेजबान इस तरह है चलो प्राप्त करना है लॉग इन करना, फ़ाइल को वर्तमान निर्देशिका में अपलोड करें .ext.tmp और अपलोड सफल होने के बाद, फ़ाइल को फ़ाइल का नाम बदलें .ext
पूरी कठिनाई, जैसा कि मुझे लगता है, उचित रूप से अनुरोध Uri के लिए सेट करना है FtpWebRe खोज।
MSDN राज्यों:
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 UserLoginDirectory/path.
ठीक है, तो मैं निम्नलिखित यूआरआई के साथ फ़ाइल अपलोड करें:
ftp.contoso.com/file.ext.tmp
बढ़िया है, फ़ाइल भूमि है जहाँ मैं यह चाहते थे होने के लिए: निर्देशिका में "उपयोगकर्ता/नाम "
अब, मैं फ़ाइल नाम बदलना चाहते हैं, तो मैं निम्नलिखित उरी के साथ वेब अनुरोध बनाने:
ftp.contoso.com/file.ext.tmp
और के रूप में पैरामीटर के लिए नाम बदलने का उल्लेख करें:
file.ext
और यह मुझे 550 त्रुटि देता है: फ़ाइल नहीं मिली, कोई अनुमति, आदि
मैं Microsoft नेटवर्क मॉनिटर में यह पता लगाया है और यह मुझे दिया:
Command: RNFR, Rename from
CommandParameter: /file.ext.tmp
Ftp: Response to Port 53724, '550 File /file.ext.tmp not found'
जैसे कि यह मूल निर्देशिका में फ़ाइल की तलाश में था - वर्तमान निर्देशिका में नहीं।
मैं मैन्युअल रूप से कुल कमांडर और फर्क सिर्फ इतना है का उपयोग कर फ़ाइल का नाम बदला कि CommandParameter पहले स्लैश के बिना किया गया था था:
CommandParameter: file.ext.tmp
मैं सफलतापूर्वक पूर्ण यूआरआई निम्नलिखित की आपूर्ति करके फ़ाइल का नाम बदलने के लिए सक्षम हूं:
ftp.contoso.com/%2fusers/%2fname/file.ext.tmp
लेकिन मुझे यह दृष्टिकोण पसंद नहीं है, क्योंकि मुझे वर्तमान उपयोगकर्ता की निर्देशिका का नाम जानना होगा। यह शायद WebRequestMethods.Ftp.PrintWorkingDirectory का उपयोग करके किया जा सकता है, लेकिन यह अतिरिक्त जटिलता जोड़ता है (निर्देशिका विधि को पुनर्प्राप्त करने के लिए इस विधि को कॉल करना, फिर उचित यूआरआई बनाने के लिए पथों को संयोजित करना)।
मुझे क्या समझ में नहीं आता है क्यों यूआरआई ftp.contoso.com/file.ext.tmp अपलोड के लिए अच्छा है और नाम बदलने के लिए नहीं है? क्या मुझसे कोई चूक हो रही है?
प्रोजेक्ट को विजुअल स्टूडियो 2010 में कोडित .NET 4.0 पर सेट किया गया है।
संपादित
ठीक है, मैं कोड स्निपेट डालने।
कृपया ध्यान दें कि FTP होस्ट, उपयोगकर्ता नाम और पासवर्ड भरना चाहिए। इस नमूने काम करने के लिए - जो है, कोई त्रुटि उत्पन्न - उपयोगकर्ता निर्देशिका जड़ से अलग ("pwd" -command कुछ "/" की तुलना में अलग लौटना चाहिए) होना चाहिए
class Program
{
private const string fileName = "test.ext";
private const string tempFileName = fileName + ".tmp";
private const string ftpHost = "127.0.0.1";
private const string ftpUserName = "anonymous";
private const string ftpPassword = "";
private const int bufferSize = 524288;
static void Main(string[] args)
{
try
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
if (!File.Exists(path))
File.WriteAllText(path, "FTP RENAME SAMPLE");
string requestUri = "ftp://" + ftpHost + "/" + tempFileName;
//upload
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(requestUri);
uploadRequest.UseBinary = true;
uploadRequest.UsePassive = true;
uploadRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
uploadRequest.KeepAlive = true;
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
Stream requestStream = null;
FileStream localFileStream = null;
localFileStream = File.OpenRead(path);
requestStream = uploadRequest.GetRequestStream();
byte[] buffer = new byte[bufferSize];
int readCount = localFileStream.Read(buffer, 0, bufferSize);
long bytesSentCounter = 0;
while (readCount > 0)
{
requestStream.Write(buffer, 0, readCount);
bytesSentCounter += readCount;
readCount = localFileStream.Read(buffer, 0, bufferSize);
System.Threading.Thread.Sleep(100);
}
localFileStream.Close();
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)uploadRequest.GetResponse();
FtpStatusCode code = response.StatusCode;
string description = response.StatusDescription;
response.Close();
if (code == FtpStatusCode.ClosingData)
Console.WriteLine("File uploaded successfully");
//rename
FtpWebRequest renameRequest = (FtpWebRequest)WebRequest.Create(requestUri);
renameRequest.UseBinary = true;
renameRequest.UsePassive = true;
renameRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
renameRequest.KeepAlive = true;
renameRequest.Method = WebRequestMethods.Ftp.Rename;
renameRequest.RenameTo = fileName;
try
{
FtpWebResponse renameResponse = (FtpWebResponse)renameRequest.GetResponse();
Console.WriteLine("Rename OK, status code: {0}, rename status description: {1}", response.StatusCode, response.StatusDescription);
renameResponse.Close();
}
catch (WebException ex)
{
Console.WriteLine("Rename failed, status code: {0}, rename status description: {1}", ((FtpWebResponse)ex.Response).StatusCode,
((FtpWebResponse)ex.Response).StatusDescription);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.ReadKey();
}
}
}
आपने अपनी समस्या का व्यापक कवरेज दिया (शायद बहुत व्यापक?), लेकिन वास्तव में आपकी मदद करने के लिए वास्तव में क्या मदद मिलेगी, विशेष रूप से वह हिस्सा जहां आप नाम बदलते हैं। ध्यान दें कि सामान्य रूप से आपको FTP नाम करते समय साइट नाम को उपसर्ग करने की आवश्यकता नहीं है। – Abel