简体   繁体   中英

FTP downloading a file from a server

I am trying to download a file from a MOXA UC8410 over FTP. My code is not working. Here is my code:

 void download2()
        {

            Uri serverUri = new Uri("ftp://169.254.1.1/CFDisk/PCPACM/pcpacm.ini");
            // The serverUri parameter should start with the ftp:// scheme.
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {

            }
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("admin", "admin");
            try
            {
                byte[] newFileData = request.DownloadData(serverUri.ToString());
                string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
                //Console.WriteLine(fileString);
            }
            catch (WebException e)
            {
                // Console.WriteLine(e.ToString());
                MessageBox.Show(e.Response + e.Message);
            }
        }

I have also tried this :

void download()
        {
            try
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://169.254.1.1:21/CFDisk/PCPACM/pcpacm.ini"));
                // using admin as the username and admin as the passward.
                request.Credentials = new NetworkCredential("admin", "admin");
                //request.KeepAlive = false;
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                processingfile(reader);
                responseStream.Close();
                reader.Close();
            }
            catch (Exception e2)
            {
                MessageBox.Show("Not connected" , e2.Message);
            }
        }

The code get to

Stream responseStream = response.GetResponseStream();

and it just stops, it never goes to the next line.

output says:

The thread 0x175c has exited with code 259 (0x103).
The thread 0x212c has exited with code 259 (0x103).

which does not help.

I can ftp to the MOXA UC8410 using the command prompt and I can download the file using FileZilla but not using my code. There is no firewall on the Moxa UC8410, so something most be wrong with my code.

UpDate: UpDATE It is working !!!

but only if I go to local Area Connection Properties and change Internet Protocol Version 4(tcp/IPv4) to

use the following IP address:

IP address: 169.254.1.5

Subnet mask: 225.225.0.0

does anyone know why? and is there a way I can fix it where I do not have to do that ?

Why do I have to put them on the same sub domain ?

Since I cannot comment yet, I'll put my questions here:

1) When you log in using FileZilla, are you starting in the directory where CFDisk lies? 2) Are you using plain-text FTP? When using FTP over TLS, the approach is slightly different. 3) Does it throw an Exception? If so, please also give us more details about that.

Also, WebResponse , Stream and StreamReader are disposable.

I created (and tested on one of my remote servers) this little Windows Form application. It seems to work fine here. Give it a look.

using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace FTPTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string localPath = @"C:\Temp\navigazione-privata.pdf";
            string ftpIPAddress = "xxx.xxx.xxx.xxx";
            string remoteFilepath = "/userdownloads/navigazione-privata.pdf";

            string ftpPath = "ftp://" + ftpIPAddress + remoteFilepath;

            using (WebClient request = new WebClient())
            {
                request.Credentials = new NetworkCredential("username", "password");
                byte[] fileData = request.DownloadData(ftpPath);

                using (FileStream file = File.Create(localPath))
                {
                    file.Write(fileData, 0, fileData.Length);
                    file.Close();
                }
                MessageBox.Show("Requested file downloaded");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM