FTP file upload

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
78
Programming Experience
Beginner
Hi,

Can I upload a file using FTP from an IP address?



When I go to windows explorer and type ftp://xxx.xx.x.xxx/

It shows me the files at that location. I know the file name and its extension.

I would like to move the file to a folder on the hard drive of my machine.

Thanks,
 
The below works to list the contents

C#:
static void Main(string[] args)
        {
            // Get the object used to communicate with the server.
            ////FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xx.x.xxx/");
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

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

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

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine($"Directory List Complete, status {response.StatusDescription}");
            Console.ReadLine();

            reader.Close();
            response.Close();
        }
 
Last edited by a moderator:
Firstly, do you want to upload or download? You say "upload" but then you describe a download.

Assuming that you want to download, can you not just call WebClient.DownloadFile?
 
It downloads into the Stream returned by GetResponseStream(). You can read the contents of that stream and copy it into a physical file if you wish, or you can copy the stream into a memory stream and just process the file contents in memory.

In the sample code there, it does not copy the file. All it does is read the contents of the stream until it gets to the end.
 
It downloads into the Stream returned by GetResponseStream(). You can read the contents of that stream and copy it into a physical file if you wish, or you can copy the stream into a memory stream and just process the file contents in memory.

In the sample code there, it does not copy the file. All it does is read the contents of the stream until it gets to the end.
Hi, yes, by trying another file in the location I saw the contents. I will try to write it to a file.
 
If you want to download from a file to a file, I ask again, can you not just call WebClient.DownloadFile? It will use a WebRequest internally anyway so, if you don't want/need to do anything specific that you can only do by using the WebRequest directly, there's no point writing all the extra code. Do it the easy way unless you need the extra flexibility such that the extra complexity is worth the effort.
 
Back
Top Bottom