Download file from ftp with webclient class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajjadlove
    New Member
    • Apr 2009
    • 19

    Download file from ftp with webclient class

    Hi there
    i use from following code for download a file from ftp server but it doesn't work......
    what's that problem ?!......
    Code:
    //FTP Class
            public event DownloadProgressChangedEventHandler DownloadProgressChanged;
            public event AsyncCompletedEventHandler DownloadFileCompleted;
    
            public void Download(string fileName,string savePath)
            {
    
                WebClient client = new WebClient();
                client.Credentials = new NetworkCredential(loginUsername, loginPass);
    
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
    
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
    
                client.DownloadFileAsync(new Uri(ftpUrl + fileName), savePath);
            }
    
            void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                if (this.DownloadProgressChanged != null) this.DownloadProgressChanged(sender, e);
            }
    
            void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
            {
                if (this.DownloadFileCompleted != null) this.DownloadFileCompleted(sender, e);
            }
    
    
    //Form Class
    
    FtpManager manager = new FtpManager(ftpUrl, username, password, FtpManager.FtpMethod.TransferFile);
    
    manager.DownloadProgressChanged += new DownloadProgressChangedEventHandler(manager_DownloadProgressChanged);
    manager.DownloadFileCompleted += new AsyncCompletedEventHandler(manager_DownloadFileCompleted);
    manager.Download(item.Text, "c:\\");
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Hi there
    i use from following code for download a file from ftp server but it don't work......
    what's that problem ?!.....
    Is this a test for the rest of us?
    Perhaps you would like to elaborate on the problems you are having.
    "Doesn't work" is a little vague. Do you get errors/exceptions? Download never starts? Download starts but you get no progress events? You get progress but never finishes?
    Have you put in breakpoints and walked through the code as it executes, checking all the values are what you expected them to be?
    Have you watched the output window for errors as it runs?

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      You might like to look at these two lines again.
      Code:
       public void Download(string fileName,string savePath)
      ...
      manager.Download(item.Text, "c:\\");
      There is also a good possibility that this will cause you trouble
      Code:
      client.DownloadFileAsync(new Uri(ftpUrl + fileName), savePath);
      since I see no effort to make sure the ftpUrl and filename will merge together in a valid path.
      EX:
      ftp:\\mywebsite .com
      +
      FileToDownload. jpg
      =
      ftp:\\mywebsite .comFileToDownl oad.jpg

      Comment

      • sajjadlove
        New Member
        • Apr 2009
        • 19

        #4
        excuse me for my bad english.....
        my problem is in its progressChanged event.

        Code:
                void manager_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
                {
                    lblUpDownStatus.Text = FormatSize(e.BytesReceived) + " from " +
                        FormatSize(e.TotalBytesToReceive) + " recieved... " +
                        e.ProgressPercentage.ToString() + "%";
                }
        e.TotalBytesRec eived is negative in its representation. .....
        why?....i don't know...

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Is the value -1 ?
          Does the value keep changing as progress is made?

          I don't do much FTP coding, so it's just a guess for me.
          If someone knows more, please chime in.

          Comment

          • sajjadlove
            New Member
            • Apr 2009
            • 19

            #6
            e.TotalBytesToR eceive is -1 and e.ProgressPerce ntage is 0.....
            with override GetWebRequest doesn't work too.......

            Code:
                class MyWebClient : WebClient
                {
                    protected override WebRequest GetWebRequest(Uri address)
                    {
                        FtpWebRequest request = (FtpWebRequest)base.GetWebRequest(address);
                        request.UsePassive = false;
                        return request;
                    }
                }

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Throughout .NET -1 is used to represent "no such value" where a positive value is expected.
              Look at a combobox. If there is a selection made, the return from "SelectedIn dex" with be the Items[] index which is zero or higher. If a combobox returns -1 for the SelectedIndex it means there is no selection made.

              I'm going to bet the reason you get -1 for the BytesToReceive is because there are no bytes to receive. The file you are trying to download couldn't be located.

              Now you have to work out *why* it couldn't be located. Bad log in credentials, no permission to download, bad file path, etc. etc.

              Comment

              Working...