469,964 Members | 1,513 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

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 ?!......
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.  
Jul 25 '09 #1
6 10692
tlhintoq
3,525 Expert 2GB
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?
Jul 25 '09 #2
tlhintoq
3,525 Expert 2GB
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
Jul 25 '09 #3
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...
Jul 25 '09 #4
tlhintoq
3,525 Expert 2GB
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.
Jul 25 '09 #5
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.  
Jul 26 '09 #6
tlhintoq
3,525 Expert 2GB
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.
Jul 26 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Ole Hanson | last post: by
2 posts views Thread by John Spiegel | last post: by
reply views Thread by Jeremy | last post: by
11 posts views Thread by belgie | last post: by
19 posts views Thread by Andrew Chalk | last post: by
6 posts views Thread by Jatin | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.