SevDer wrote:
> Hi,
>
> I am trying to write a simple application which copies the predefined
> files (hardcoded in the code) to my ftp server.
>
> I couldn't make it working. I don't want to use any 3rd party
> components, because I want to learn. (I am using .NET Framework 2.0,
but
> I don't think my problem is because of 2.0)
>
> ########################################
> My code fails at line:
> Stream requestStream = myFtpRequest.GetRequestStream();
>
> ########################################
> And the error message is as follows:
> The remote server returned an error: (550) Requested action not
taken.
> File unavailable (e.g., file not found, no access)..
>
> The file that I want to upload may or may not exist in the
> destionation. (For testing I put it there also, but didn't work)
>
> ########################################
> The method I am using is as follows (got it from MSDN help files)
>
> public static bool UploadToFTP()
> {
> // The URI described by serverUri should use the ftp://
scheme.
> // It contains the name of the file on the server.
> // Example: ftp://contoso.com/someFile.txt.
> //
> // The fileName parameter identifies the file containing
> the data to be uploaded.
> Uri sevderComFtp = new
> Uri("ftp://sevder.com/mysubfolder/myfiletoupload.ext");
>
> string assemblyPath = @"C:\my folder\sub
> folder\myfiletoupload.ext";
>
> if (sevderComFtp.Scheme != Uri.UriSchemeFtp)
> {
> return false;
> }
>
> // Get the object used to communicate with the server.
> FtpWebRequest myFtpRequest =
> (FtpWebRequest)WebRequest.Create(sevderComFtp);
> myFtpRequest.Method = FtpMethods.UploadFile;
> myFtpRequest.Credentials = new
> NetworkCredential("username", "password");
>
> // Don't set a time limit for the operation to complete.
> myFtpRequest.Timeout =
System.Threading.Timeout.Infinite;
>
> // Copy the file contents to the request stream.
> const int bufferLength = 2048;
> byte[] buffer = new byte[bufferLength];
>
> int count = 0;
> int readBytes = 0;
> FileStream stream = File.OpenRead(assemblyPath);
>
>
> Stream requestStream = myFtpRequest.GetRequestStream();
>
> do
> {
> readBytes = stream.Read(buffer, 0, bufferLength);
> requestStream.Write(buffer, 0, bufferLength);
> count += readBytes;
> }
> while (readBytes != 0);
>
> Console.WriteLine("Writing {0} bytes to the stream.",
count);
> // IMPORTANT: Close the request stream before sending
the
> request.
> requestStream.Close();
>
> FtpWebResponse response =
> (FtpWebResponse)myFtpRequest.GetResponse();
> Console.WriteLine("Upload status: {0}",
response.Status);
>
> response.Close();
> return true;
> }
> ########################################
>
> --
>
> SevDer
>
http://www.sevder.com