Connect with Expertise | Find Experts, Get Answers, Share Insights

Download file from ftp with webclient class

 
Join Date: Apr 2009
Posts: 19
#1: Jul 25 '09
Hi there
i use from following code for download a file from ftp server but it doesn't work......
what's that problem ?!......
Expand|Select|Wrap|Line Numbers
  1. //FTP Class
  2.         public event DownloadProgressChangedEventHandler DownloadProgressChanged;
  3.         public event AsyncCompletedEventHandler DownloadFileCompleted;
  4.  
  5.         public void Download(string fileName,string savePath)
  6.         {
  7.  
  8.             WebClient client = new WebClient();
  9.             client.Credentials = new NetworkCredential(loginUsername, loginPass);
  10.  
  11.             client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  12.  
  13.             client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  14.  
  15.             client.DownloadFileAsync(new Uri(ftpUrl + fileName), savePath);
  16.         }
  17.  
  18.         void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  19.         {
  20.             if (this.DownloadProgressChanged != null) this.DownloadProgressChanged(sender, e);
  21.         }
  22.  
  23.         void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  24.         {
  25.             if (this.DownloadFileCompleted != null) this.DownloadFileCompleted(sender, e);
  26.         }
  27.  
  28.  
  29. //Form Class
  30.  
  31. FtpManager manager = new FtpManager(ftpUrl, username, password, FtpManager.FtpMethod.TransferFile);
  32.  
  33. manager.DownloadProgressChanged += new DownloadProgressChangedEventHandler(manager_DownloadProgressChanged);
  34. manager.DownloadFileCompleted += new AsyncCompletedEventHandler(manager_DownloadFileCompleted);
  35. manager.Download(item.Text, "c:\\");
  36.  
  37.  

tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#2: Jul 25 '09

re: Download file from ftp with webclient class


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?
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#3: Jul 25 '09

re: Download file from ftp with webclient class


You might like to look at these two lines again.
Expand|Select|Wrap|Line Numbers
  1.  public void Download(string fileName,string savePath)
  2. ...
  3. manager.Download(item.Text, "c:\\");
  4.  
There is also a good possibility that this will cause you trouble
Expand|Select|Wrap|Line Numbers
  1. 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.comFileToDownload.jpg
 
Join Date: Apr 2009
Posts: 19
#4: Jul 25 '09

re: Download file from ftp with webclient class


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

Expand|Select|Wrap|Line Numbers
  1.         void manager_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  2.         {
  3.             lblUpDownStatus.Text = FormatSize(e.BytesReceived) + " from " +
  4.                 FormatSize(e.TotalBytesToReceive) + " recieved... " +
  5.                 e.ProgressPercentage.ToString() + "%";
  6.         }
  7.  
e.TotalBytesReceived is negative in its representation......
why?....i don't know...
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#5: Jul 25 '09

re: Download file from ftp with webclient class


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.
 
Join Date: Apr 2009
Posts: 19
#6: Jul 26 '09

re: Download file from ftp with webclient class


e.TotalBytesToReceive is -1 and e.ProgressPercentage is 0.....
with override GetWebRequest doesn't work too.......

Expand|Select|Wrap|Line Numbers
  1.     class MyWebClient : WebClient
  2.     {
  3.         protected override WebRequest GetWebRequest(Uri address)
  4.         {
  5.             FtpWebRequest request = (FtpWebRequest)base.GetWebRequest(address);
  6.             request.UsePassive = false;
  7.             return request;
  8.         }
  9.     }
  10.  
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#7: Jul 26 '09

re: Download file from ftp with webclient class


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 "SelectedIndex" 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.
Reply