0

i want to upload a file to server and here is my code which do not work

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.com/myfolder");
         request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("aaa", "aaa");

        // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader("c://a.txt");
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();

i can access ftp through filezilla with these credentials.nothing is uploading in this case. Now when i want to upload to another folder inside like ftp://mysite.com/myfolder/anotherfolder if give me error that file or folder not availble etc etc any help?

1 Answer 1

1

Your error is presenting that folder doesn't exist either on sever or local. Please make sure that folder which is you are trying to access already exist on server and client.

Sign up to request clarification or add additional context in comments.

1 Comment

i am writing this comment for others as this point saved alot of my time...the isue was in FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp.mysite.com/t/a.txt)...also we do not need to have a.txt in the folder as source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.